File: MyEditorComponent.java

package info (click to toggle)
libjgraph-java 5.12.4.2%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 7,884 kB
  • ctags: 7,321
  • sloc: java: 20,619; xml: 135; sh: 51; makefile: 10
file content (159 lines) | stat: -rw-r--r-- 4,894 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.jgraph.example.mycellmodeleditor;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.CellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

/**
 * A basic example JComponent to in place edit both the cell model (user object)
 * and the cell label. First of all you have the opportunity to edit the cell
 * label. Also you can choose the cell user object inside a JTree. A JTree is
 * fine to allow the user to choose within a hierarchy, but you could have
 * choosen a combo box instead for instance. You can choose whether you want to
 * use the label feature or if you rather prefer to use the toString method of
 * the user object instead. Finally you can confirm or cancel your choice.
 * 
 * @author rvalyi
 */
public class MyEditorComponent extends JComponent implements ActionListener,
		ItemListener, TreeSelectionListener {
	private static JPanel labelPanel = new JPanel();

	private static JPanel modelPanel = new JPanel();

	private static JPanel actionPanel = new JPanel();

	private static JButton okButton = new JButton("OK");

	private static JButton cancelButton = new JButton("Cancel");

	private static JCheckBox useModelLabel = new JCheckBox("use model label");

	private static JTextField labelField = new JTextField();

	private static JTree chooserTree = new JTree();

	/**
	 * A temporary clone of the business object to work with before commiting
	 * the change (allows to undo).
	 */
	private static BusinessObjectWrapper newModel;

	/**
	 * the old user object
	 */
	private static BusinessObjectWrapper oldModel;

	/**
	 * a reference to the editor: here we only use it to force the end of the
	 * editing after the OK button or Cancel button is pressed
	 */
	public static CellEditor cellEditor;

	public MyEditorComponent(CellEditor cellEditor) {
		MyEditorComponent.cellEditor = cellEditor;
		labelField.setColumns(10);
		labelPanel.add(labelField);
		labelPanel.add(useModelLabel);
		modelPanel.add(chooserTree);
		actionPanel.add(okButton);
		actionPanel.add(cancelButton);
		okButton.addActionListener(this);
		cancelButton.addActionListener(this);
		okButton.setActionCommand("ok");
		cancelButton.setActionCommand("cancel");
		this.setLayout(new BorderLayout());
		this.add(labelPanel, BorderLayout.NORTH);
		this.add(new JScrollPane(modelPanel), BorderLayout.CENTER);
		this.add(actionPanel, BorderLayout.SOUTH);
		chooserTree.addTreeSelectionListener(this);
		chooserTree.getSelectionModel().setSelectionMode(
				TreeSelectionModel.SINGLE_TREE_SELECTION);
		useModelLabel.addItemListener(this);
	}

	/**
	 * the editor is lightweight so you need to install the graph cell
	 * properties before using it.
	 * 
	 * @param value
	 */
	public void installValue(BusinessObjectWrapper value) {
		oldModel = value;
		newModel = value;
		labelField.setText(new String(newModel.getLabel()));
		labelField.setEnabled(true);
		useModelLabel.setSelected(false);
		chooserTree.setSelectionPath(null);
		if (newModel.getValue() != null)
			chooserTree.setSelectionPath(new TreePath(
					((DefaultTreeModel) chooserTree.getModel())
							.getPathToRoot(newModel.getValue())));
	}

	public BusinessObjectWrapper getValue() {
		return oldModel;
	}

	/**
	 * Unique action entry point dispatching several component specific actions
	 */
	public void actionPerformed(ActionEvent e) {
		if ("ok".equals(e.getActionCommand())) {
			oldModel = newModel;
			oldModel.setLabel(labelField.getText());
			cellEditor.stopCellEditing();
		} else {
			cellEditor.stopCellEditing();
		}
	}

	/**
	 * the tree selection listener
	 */
	public void valueChanged(TreeSelectionEvent e) {
		if (chooserTree.isSelectionEmpty())
			return;
		TreePath[] treeSelection = chooserTree.getSelectionModel()
				.getSelectionPaths();
		newModel.setValue((DefaultMutableTreeNode) treeSelection[0]
				.getLastPathComponent());
		if (!useModelLabel.isSelected()) {
			newModel.setLabel(treeSelection[0].getLastPathComponent()
					.toString());
			labelField.setText(newModel.toString());
		}
	}

	/**
	 * the checkbox listener
	 */
	public void itemStateChanged(ItemEvent e) {
		if (!useModelLabel.isSelected()) {
			if (newModel.getValue() != null)
				labelField.setText(newModel.getValue().toString());
			labelField.setEnabled(false);
		} else {
			labelField.setEnabled(true);
			labelPanel.requestFocus();
		}
	}

}