001: package org.enhydra.jawe.components.simplenavigator;
002:
003: import java.awt.BorderLayout;
004: import java.awt.Color;
005: import java.awt.Point;
006: import java.awt.Rectangle;
007: import java.awt.event.MouseAdapter;
008: import java.awt.event.MouseEvent;
009: import java.awt.event.MouseListener;
010: import java.util.ArrayList;
011: import java.util.Iterator;
012: import java.util.List;
013:
014: import javax.swing.BorderFactory;
015: import javax.swing.JComponent;
016: import javax.swing.JPanel;
017: import javax.swing.JPopupMenu;
018: import javax.swing.JScrollPane;
019: import javax.swing.JToolBar;
020: import javax.swing.JTree;
021: import javax.swing.JViewport;
022: import javax.swing.ScrollPaneConstants;
023: import javax.swing.SwingUtilities;
024: import javax.swing.tree.TreeModel;
025: import javax.swing.tree.TreePath;
026: import javax.swing.tree.TreeSelectionModel;
027:
028: import org.enhydra.jawe.BarFactory;
029: import org.enhydra.jawe.JaWEComponent;
030: import org.enhydra.jawe.JaWEComponentView;
031: import org.enhydra.jawe.JaWEManager;
032: import org.enhydra.jawe.XPDLElementChangeInfo;
033: import org.enhydra.jawe.base.controller.JaWEActions;
034: import org.enhydra.jawe.components.XPDLTreeCellRenderer;
035: import org.enhydra.jawe.components.XPDLTreeModel;
036: import org.enhydra.jawe.components.XPDLTreeNode;
037: import org.enhydra.shark.xpdl.XMLCollection;
038: import org.enhydra.shark.xpdl.XMLElement;
039: import org.enhydra.shark.xpdl.XMLElementChangeInfo;
040: import org.enhydra.shark.xpdl.elements.Activities;
041: import org.enhydra.shark.xpdl.elements.Activity;
042: import org.enhydra.shark.xpdl.elements.ActivitySet;
043: import org.enhydra.shark.xpdl.elements.ActivitySets;
044: import org.enhydra.shark.xpdl.elements.Package;
045: import org.enhydra.shark.xpdl.elements.Transition;
046: import org.enhydra.shark.xpdl.elements.Transitions;
047: import org.enhydra.shark.xpdl.elements.WorkflowProcess;
048: import org.enhydra.shark.xpdl.elements.WorkflowProcesses;
049:
050: /**
051: * Used to display Package hierarchy tree.
052: *
053: * @author Sasa Bojanic
054: */
055: public class SimpleNavigatorPanel extends JPanel implements
056: JaWEComponentView {
057:
058: protected XPDLTreeModel treeModel;
059:
060: protected JTree tree;
061:
062: protected JToolBar toolbar;
063: protected JScrollPane scrollPane;
064: protected SimpleNavigator controller;
065: protected int xClick, yClick;
066:
067: protected MouseListener mouseListener;
068: protected XPDLTreeCellRenderer renderer;
069:
070: public SimpleNavigatorPanel(SimpleNavigator controller) {
071: this .controller = controller;
072: }
073:
074: public void configure() {
075: // setLayout(new BoxLayout(this,BoxLayout.X_AXIS));
076: setBorder(BorderFactory.createEtchedBorder());
077: setLayout(new BorderLayout());
078: toolbar = BarFactory
079: .createToolbar("defaultToolbar", controller);
080: toolbar.setFloatable(false);
081: if (toolbar.getComponentCount() > 0) {
082: add(toolbar, BorderLayout.NORTH);
083: }
084: init();
085: }
086:
087: public void printTreeModel() {
088: printTreeModel(treeModel.getRootNode());
089: }
090:
091: public void printTreeModel(XPDLTreeNode n) {
092: System.err.println("There are " + n.getChildCount()
093: + " children for " + n.getXPDLElement());
094: for (int i = 0; i < n.getChildCount(); i++) {
095: printTreeModel((XPDLTreeNode) n.getChildAt(i));
096: }
097:
098: }
099:
100: public void init() {
101: controller.getSettings().adjustActions();
102: treeModel = new SimpleNavigatorTreeModel(controller);
103:
104: tree = new JTree(treeModel) {
105: public void scrollRectToVisible(Rectangle aRect) {
106: aRect.x = scrollPane.getHorizontalScrollBar()
107: .getValue();
108: super .scrollRectToVisible(aRect);
109: }
110: };
111:
112: // setting some tree properties
113: tree.getSelectionModel().setSelectionMode(
114: TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
115: tree.setRootVisible(false);
116: tree.setShowsRootHandles(true);
117: renderer = new XPDLTreeCellRenderer(controller);
118: Color bckColor = controller.getNavigatorSettings()
119: .getBackGroundColor();
120:
121: renderer.setBackgroundNonSelectionColor(bckColor);
122: renderer.setBackgroundSelectionColor(controller
123: .getNavigatorSettings().getSelectionColor());
124: tree.setBackground(bckColor);
125: tree.setCellRenderer(renderer);
126: tree.addTreeSelectionListener(controller);
127:
128: /** MouseListener for JTree */
129: mouseListener = new MouseAdapter() {
130: public void mouseClicked(MouseEvent me) {
131: xClick = me.getX();
132: yClick = me.getY();
133: TreePath path = tree.getPathForLocation(xClick, yClick);
134:
135: if (path != null) {
136: tree.setAnchorSelectionPath(path);
137:
138: if (SwingUtilities.isRightMouseButton(me)) {
139: if (!tree.isPathSelected(path)) {
140: tree.setSelectionPath(path);
141: }
142:
143: JPopupMenu popup = BarFactory.createPopupMenu(
144: "default", controller);
145:
146: popup.show(tree, me.getX(), me.getY());
147: }
148:
149: XPDLTreeNode node = (XPDLTreeNode) path
150: .getLastPathComponent();
151:
152: if (me.getClickCount() > 1
153: && !SwingUtilities.isRightMouseButton(me)
154: && tree.getModel().isLeaf(node)) {
155: JaWEManager
156: .getInstance()
157: .getJaWEController()
158: .getJaWEActions()
159: .getAction(
160: JaWEActions.EDIT_PROPERTIES_ACTION)
161: .actionPerformed(null);
162: }
163: } else {
164: TreePath close = tree.getClosestPathForLocation(
165: xClick, yClick);
166: Rectangle rect = tree.getPathBounds(close);
167: if (rect == null
168: || !(rect.y < yClick && rect.y
169: + rect.height > yClick)) {
170: JaWEManager.getInstance().getJaWEController()
171: .getSelectionManager().setSelection(
172: (XMLElement) null, false);
173: tree.clearSelection();
174: }
175: }
176:
177: tree.getParent().requestFocus();
178: }
179: };
180:
181: tree.addMouseListener(mouseListener);
182:
183: // creates panel
184: scrollPane = new JScrollPane();
185: scrollPane
186: .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
187: scrollPane
188: .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
189: scrollPane.setViewportView(tree);
190: JViewport port = scrollPane.getViewport();
191: port.setScrollMode(JViewport.BLIT_SCROLL_MODE);
192: scrollPane.setBackground(Color.lightGray);
193: //scrollPane.getVerticalScrollBar().setUnitIncrement(20);
194: // scrollPane.getHorizontalScrollBar().setUnitIncrement(40);
195:
196: add(scrollPane, BorderLayout.CENTER);
197: }
198:
199: public TreeModel getTreeModel() {
200: return treeModel;
201: }
202:
203: public JTree getTree() {
204: return tree;
205: }
206:
207: public JaWEComponent getJaWEComponent() {
208: return controller;
209: }
210:
211: public JComponent getDisplay() {
212: return this ;
213: }
214:
215: public Point getMouseClickLocation() {
216: return new Point(xClick, yClick);
217: }
218:
219: public void handleXPDLChangeEvent(XPDLElementChangeInfo info) {
220: int action = info.getAction();
221: XMLElement el = info.getChangedElement();
222: List l = info.getChangedSubElements();
223:
224: tree.removeTreeSelectionListener(controller);
225:
226: if (action == XPDLElementChangeInfo.SELECTED) {
227: tree.clearSelection();
228: }
229:
230: if (el instanceof Activity
231: || el instanceof Package
232: || el instanceof WorkflowProcesses
233: || el instanceof WorkflowProcess
234: || el instanceof ActivitySets
235: || el instanceof ActivitySet
236: || el instanceof Activities
237: || (el instanceof Transitions && action == XPDLElementChangeInfo.SELECTED)) {
238: if (action == XMLElementChangeInfo.INSERTED) {
239: if (l != null && l.size() > 0) {
240: Iterator it1 = l.iterator();
241: while (it1.hasNext()) {
242: treeModel.insertNode((XMLElement) it1.next());
243: }
244: } else {
245: if (el instanceof Package) {
246: treeModel.insertNode(el);
247: }
248: }
249: if (el instanceof Package) {
250: controller.getSettings().adjustActions();
251: }
252: } else if (action == XMLElementChangeInfo.REMOVED) {
253: if (l != null && l.size() > 0) {
254: Iterator it1 = l.iterator();
255: while (it1.hasNext()) {
256: treeModel.removeNode((XMLElement) it1.next());
257: }
258: } else {
259: treeModel.removeNode(el);
260: }
261: if (treeModel.getRootNode().getChildCount() == 0) {
262: reinitialize();
263: return;
264: }
265: } else if (action == XPDLElementChangeInfo.SELECTED) {
266: if (el != null) {
267: List toSelect = new ArrayList();
268: if (l != null) {
269: toSelect.addAll(l);
270: }
271: if (toSelect.size() == 0) {
272: toSelect.add(el);
273: }
274: for (int i = 0; i < toSelect.size(); i++) {
275: XMLElement toSel = (XMLElement) toSelect.get(i);
276: if (toSelect instanceof Transitions
277: || toSelect instanceof Transition) {
278: continue;
279: }
280: XPDLTreeNode n = treeModel.findNode(toSel);
281: TreePath tp = null;
282: if (n != null) {
283: tp = new TreePath(n.getPath());
284: tree.addSelectionPath(tp);
285: }
286: if (tp != null) {
287: tree.scrollPathToVisible(tp);
288: }
289: }
290: }
291: } else if (action == XMLElementChangeInfo.REPOSITIONED) {
292: List elsToReposition = new ArrayList();
293: List newPositions = new ArrayList();
294: if (el instanceof XMLCollection) {
295: if (l != null) {
296: elsToReposition.addAll(l);
297: newPositions.addAll((List) info.getNewValue());
298: }
299: for (int j = 0; j < elsToReposition.size(); j++) {
300: XMLElement eltr = (XMLElement) elsToReposition
301: .get(j);
302: treeModel.repositionNode(eltr,
303: ((Integer) newPositions.get(j))
304: .intValue());
305: }
306: }
307: }
308: }
309:
310: tree.addTreeSelectionListener(controller);
311: }
312:
313: protected void reinitialize() {
314: remove(scrollPane);
315: treeModel.clearTree();
316: tree.getSelectionModel().clearSelection();
317: tree.removeMouseListener(mouseListener);
318: tree.removeTreeSelectionListener(controller);
319: tree.setCellRenderer(null);
320: init();
321: }
322:
323: // before doing this, listener has to be removed
324: public void setCurrentSelection() {
325: List toSelect = JaWEManager.getInstance().getJaWEController()
326: .getSelectionManager().getSelectedElements();
327: for (int i = 0; i < toSelect.size(); i++) {
328: XMLElement toSel = (XMLElement) toSelect.get(i);
329: if (toSel instanceof Package
330: || toSel instanceof WorkflowProcess
331: || toSel instanceof ActivitySet
332: || toSel instanceof Activity) {
333: XPDLTreeNode n = treeModel.findNode(toSel);
334: TreePath tp = null;
335: if (n != null) {
336: tp = new TreePath(n.getPath());
337: tree.addSelectionPath(tp);
338: }
339: if (tp != null) {
340: tree.scrollPathToVisible(tp);
341: }
342: }
343: }
344: }
345:
346: }
|