Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
InspectorNavigator.java
1 
21 package net.squiz.matrix.inspector;
22 
23 import net.squiz.matrix.matrixtree.*;
24 import net.squiz.matrix.core.*;
25 import net.squiz.matrix.ui.*;
26 import javax.swing.*;
27 import java.awt.*;
28 import java.awt.event.*;
29 import javax.swing.tree.*;
30 import java.util.List;
31 import java.util.ArrayList;
32 
39 class InspectorNavigator extends JPanel {
40 
41  private JButton backBtn;
42  private JButton forwardBtn;
43  private ButtonMenu showPathBtn;
44  private InspectorGadget inspector;
45  private TreePath current;
46  private List backPath = new ArrayList();
47  private List forwardPath = new ArrayList();
48 
49  //{{{ Public Methods
50 
57  public InspectorNavigator(InspectorGadget inspector/*, TreePath current*/) {
58  this.inspector = inspector;
59  setBackground(UIManager.getColor("InspectorNavigator.background"));
60  setLayout(new FlowLayout(FlowLayout.LEADING));
61  init();
62  }
63 
67  public void setCurrentPath(TreePath currentPath) {
68  current = currentPath;
69  }
70 
76  public void setBackPath(TreePath path) {
77  if (current != null) backPath.add(current);
78  current = path;
79  forwardPath.clear();
80  updateBackForward();
81  }
82 
88  public void updateBackForward() {
89  if (backPath.size() == 0) backBtn.setEnabled(false);
90  else backBtn.setEnabled(true);
91  if (forwardPath.size() == 0) forwardBtn.setEnabled(false);
92  else forwardBtn.setEnabled(true);
93  if (current.getPathCount() < 2)
94  showPathBtn.setEnabled(false);
95  else showPathBtn.setEnabled(true);
96 
97  Object[] treePathObjects = new Object[current.getPathCount() - 1];
98  for (int k = 1; k < current.getPathCount(); k++) {
99  treePathObjects[k-1] = current.getPathComponent(k);
100  }
101 
102  showPathBtn.setToolTipText(convertPathToString(new TreePath(treePathObjects)));
103  }
104 
108  public void disablePanel() {
109  forwardBtn.setEnabled(false);
110  backBtn.setEnabled(false);
111  showPathBtn.setEnabled(false);
112  }
113 
114  //}}}
115 
116  //{{{ Protected Methods
117  //}}}
118 
119  //{{{ Package Private Methods
120  //}}}
121 
122  //{{{ Private Methods
123 
127  private void init() {
128  setSize(240, 40);
129  setBackground(UIManager.getColor("InspectorNavigator.background"));
130 
131  ActionListener btnListener = new ButtonActionListener();
132 
133  backBtn = (JButton) createButton("back", btnListener, "Back", false);
134  forwardBtn = (JButton) createButton("forward", btnListener, "Forward", false);
135  showPathBtn = (ButtonMenu) createButton("show_path", btnListener, "Show Path", true);
136 
137  add(backBtn);
138  add(forwardBtn);
139  add(showPathBtn);
140 
141  disablePanel();
142  }
143 
157  private AbstractButton createButton(
158  String iconName,
159  ActionListener listener,
160  String toolTipText,
161  boolean buttonMenu) {
162 
163  Icon icon = GUIUtilities.getAssetMapIcon(iconName + ".png");
164  Icon disabledIcon = GUIUtilities.getAssetMapIcon(iconName + "_disabled.png");
165  Icon pressedIcon = GUIUtilities.getAssetMapIcon(iconName + "_on.png");
166 
167  AbstractButton button = null;
168  if (buttonMenu)
169  button = new ButtonMenu(icon, pressedIcon);
170  else
171  button = new JButton(icon);
172 
173  button.setDisabledIcon(disabledIcon);
174  button.setPressedIcon(pressedIcon);
175  button.setBorderPainted(false);
176 
177  button.setPreferredSize(new Dimension(icon.getIconWidth(), icon.getIconHeight()));
178 
179  button.addActionListener(listener);
180  button.setToolTipText(toolTipText);
181 
182  return button;
183  }
184 
189  private void navigateBack() {
190  int backLastIndex = backPath.size() - 1;
191  TreePath oldPath = (TreePath) backPath.get(backLastIndex);
192  inspector.populateInspector(oldPath);
193  forwardPath.add(current);
194  current = oldPath;
195  backPath.remove(backLastIndex);
196  updateBackForward();
197  }
198 
203  private void navigateNext() {
204  int forwardLastIndex = forwardPath.size() - 1;
205  TreePath oldPath = (TreePath) forwardPath.get(forwardLastIndex);
206  inspector.populateInspector(oldPath);
207  backPath.add(current);
208  current = oldPath;
209  forwardPath.remove(forwardLastIndex);
210  updateBackForward();
211  }
212 
216  private String convertPathToString(TreePath path) {
217  return convertPathToString(path.getPath());
218  }
219 
220  private String convertPathToString(Object[] nodes) {
221  String pathStr = "";
222 
223  for (int i = 0; i < nodes.length; i++) {
224  pathStr = pathStr + ((MatrixTreeNode) nodes[i]).getName();
225  if (i != nodes.length - 1)
226  pathStr = pathStr + " > ";
227  }
228  return pathStr;
229  }
230 
231  //}}}
232 
233  //{{{ Protected Inner Classes
234  //}}}
235 
236  //{{{ Inner Classes
237 
244  class ButtonActionListener implements ActionListener {
245 
246  private boolean isVisible = false;
247  private JPopupMenu menu;
253  public void actionPerformed(ActionEvent evt) {
254  Object source = evt.getSource();
255 
256  if (source == backBtn) navigateBack();
257  else if (source == forwardBtn) navigateNext();
258  else if (source == showPathBtn) {
259  showPathBtn.setPopupMenu(getMenu());
260  }
261  }
262 
263  private JPopupMenu getMenu() {
264  JPopupMenu menu = new JPopupMenu();
265 
266  ActionListener matrixListener = new ActionListener() {
267  public void actionPerformed(ActionEvent e) {
268  MatrixTreeNode rootNode
269  = (MatrixTreeNode) AssetManager.getRootFolderNode();
270  TreePath rootPath = inspector.getTree().getPathToRoot(rootNode);
271  inspector.populateInspector(rootPath);
272  setBackPath(rootPath);
273  }
274  };
275 
276  JMenuItem matrixItem = new JMenuItem("My Matrix System");
277  matrixItem.addActionListener(matrixListener);
278  menu.add(matrixItem);
279 
280  if (current.getPathCount() > 1) menu.addSeparator();
281 
282  for (int i = 1; i < current.getPathCount(); i++) {
283  final TreePath path = inspector.getTree().getPathToRoot(
284  (MatrixTreeNode) current.getPathComponent(i));
285 
286  Object[] treePathObjects = new Object[path.getPathCount() - 1];
287  for (int k = 1; k < path.getPathCount(); k++)
288  treePathObjects[k - 1] = path.getPathComponent(k);
289 
290  String pathStr = convertPathToString(treePathObjects);
291  ActionListener pathListener = new ActionListener() {
292  public void actionPerformed(ActionEvent evt) {
293  inspector.populateInspector(path);
294  setBackPath(path);
295  }
296  };
297 
298  MatrixTreeNode node = (MatrixTreeNode) path.getLastPathComponent();
299  Icon icon = node.getAsset().getType().getIcon();
300  JMenuItem pathItem = new JMenuItem(pathStr);
301  pathItem.addActionListener(pathListener);
302  pathItem.setIcon(icon);
303 
304  menu.add(pathItem);
305  }
306 
307  return menu;
308  }
309 
310  }//end class ButtonActionListener
311 
312  //}}}
313 
314 }//end class InspectorNavigator