Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
SelectionDialog.java
1 
16 package net.squiz.matrix.ui;
17 
18 import net.squiz.matrix.matrixtree.*;
19 import net.squiz.matrix.core.*;
20 import javax.swing.*;
21 import javax.swing.tree.*;
22 import java.awt.*;
23 import java.awt.event.*;
24 import javax.swing.border.*;
25 
29 public class SelectionDialog extends MatrixDialog implements MatrixConstants {
30 
31  private SelectionTree selectionTree;
32  private DefaultMutableTreeNode parentNode;
33  private JPopupMenu removeMenu;
34 
35  /*
36  * Constructs the selection dialog
37  */
38  private SelectionDialog() {
39  JPanel contentPane = new JPanel(new BorderLayout());
40  setContentPane(contentPane);
41 
42  Border border = new CompoundBorder(
43  new EmptyBorder(12, 12, 12, 12),
44  new TitledBorder(Matrix.translate("asset_map_dialog_title_current_selection"))
45  );
46  Border border2 = new CompoundBorder(
47  border,
48  new EmptyBorder(8, 8, 8, 8)
49  );
50  contentPane.setBorder(border2);
51  contentPane.add(getSelectionPanel());
52 
53  // create a menu for removing nodes
54  JMenuItem removeItem = new JMenuItem(Matrix.translate("asset_map_menu_remove"));
55  removeItem.addActionListener(new removeSelectionNodesAction());
56  removeMenu = new JPopupMenu();
57  removeMenu.add(removeItem);
58 
59  setSize(200, 300);
60  }
61 
68  SelectionDialog selectionDialog = null;
70  selectionDialog = new SelectionDialog();
71  MatrixDialog.putDialog(selectionDialog);
72  } else {
73  // bring the selection dialog to the front
74  selectionDialog = (SelectionDialog) MatrixDialog.getDialog(SelectionDialog.class);
75  selectionDialog.toFront();
76  }
77  return selectionDialog;
78  }
79 
80  /*
81  * We have this method because we want to update the model every
82  * time except when we create the tree.
83  */
84  private void setNodes(MatrixTreeNode[] nodes, boolean updateModel) {
85  if (nodes == null)
86  return;
87  parentNode.removeAllChildren();
88  for (int i = 0; i < nodes.length; i++) {
89  parentNode.add(nodes[i]);
90  }
91  if (updateModel) {
92  DefaultTreeModel model = (DefaultTreeModel) selectionTree.getModel();
93  model.nodeStructureChanged(parentNode);
94  }
95  }
96 
102  public void setNodes(MatrixTreeNode[] nodes) {
103  setNodes(nodes, true);
104  }
105 
110  public void removeAllNodes() {
111  parentNode.removeAllChildren();
112  ((DefaultTreeModel) selectionTree.getModel()).nodeStructureChanged(parentNode);
113  }
114 
115  /*
116  * Returns a panel with the tree and two buttons for removing nodes
117  * from the selection. The tree extends MatrixTree so the visual
118  * selection tool can be used to select nodes
119  */
120  private JPanel getSelectionPanel() {
121  MatrixTreeNode[] nodes = Selection.getNodes();
122 
123  if (nodes != null) {
124  parentNode = new DefaultMutableTreeNode();
125  setNodes(nodes, false);
126  selectionTree = new SelectionTree(new DefaultTreeModel(parentNode));
127  selectionTree.setRootVisible(false);
128  }
129  selectionTree.setCellRenderer(MatrixTreeBus.getCellRenderer());
130 
131  JScrollPane treePane = new JScrollPane(selectionTree);
132  JButton removeAllBtn = new JButton(Matrix.translate("asset_map_button_remove_all"));
133  JButton removeSelectionBtn = new JButton(Matrix.translate("asset_map_button_remove_selection"));
134 
135  removeAllBtn.addActionListener(new removeAllNodesAction());
136  removeSelectionBtn.addActionListener(new removeSelectionNodesAction());
137 
138  GridBagLayout gb = new GridBagLayout();
139  GridBagConstraints c = new GridBagConstraints();
140  JPanel selectionPanel = new JPanel(gb);
141 
142  // add the selection panel so that it is maximum width and height
143  c.fill = GridBagConstraints.BOTH;
144  c.gridwidth = GridBagConstraints.REMAINDER;
145  c.weightx = 1.0;
146  c.weighty = 1.0;
147  gb.setConstraints(treePane, c);
148  selectionPanel.add(treePane);
149 
150  // add the buttons so they are maximum width and minimum height
151  c.weightx = 0.0;
152  c.weighty = 0.0;
153  gb.setConstraints(removeAllBtn, c);
154  selectionPanel.add(removeAllBtn);
155  gb.setConstraints(removeSelectionBtn, c);
156  selectionPanel.add(removeSelectionBtn);
157 
158  return selectionPanel;
159  }
160 
161  // inner classes
162 
166  class removeSelectionNodesAction implements ActionListener {
167  public void actionPerformed(ActionEvent evt) {
168  TreePath[] paths = selectionTree.getSelectionPaths();
169  if (paths == null)
170  return;
171 
172  for (int i = 0; i < paths.length; i++) {
173  MatrixTreeNode node = (MatrixTreeNode) paths[i].getLastPathComponent();
174  ((DefaultTreeModel) selectionTree.getModel()).removeNodeFromParent(node);
175  }
176  Selection.removeNodes(paths);
177  }
178  }//end class removeSelectionNodesAction
179 
183  class removeAllNodesAction implements ActionListener {
184  public void actionPerformed(ActionEvent evt) {
185  removeAllNodes();
186  Selection.removeAllNodes();
187  }
188  }//end class removeAllNodesAction
189 
195  class SelectionTree extends MatrixTree {
196 
200  public SelectionTree(TreeModel model) {
201  super(model);
202  setMoveEnabled(false);
203  }
204 
209  protected MenuHandler getMenuHandler() {
210  return new SelectionTreeMenuHandler();
211  }
212 
217  protected class SelectionTreeMenuHandler extends MenuHandler {
218 
223  protected JPopupMenu getMenuForSingleSelection() {
224  return removeMenu;
225  }
226 
231  protected JPopupMenu getMenuForMultipleSelection() {
232  return removeMenu;
233  }
234 
239  protected JPopupMenu getMenuForVoidSpace() {
240  if (!isEmptySelection())
241  return removeMenu;
242  return null;
243  }
244 
249  protected JMenuItem[] getAncillaryMenuItems() {
250  return null;
251  }
252 
256  protected void setKeyBoardsActions() {}
257 
258  }
259  }//end class SelectionTree
260 }