001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.cleverphl.gui;
025:
026: import jacareto.cleverphl.CleverPHL;
027: import jacareto.cleverphl.session.Session;
028: import jacareto.struct.RootElement;
029: import jacareto.struct.StructureElement;
030: import jacareto.struct.StructureTree;
031: import jacareto.system.Environment;
032: import jacareto.toolkit.swing.PopupListener;
033:
034: import java.awt.Color;
035: import java.awt.Component;
036: import java.awt.FlowLayout;
037: import java.awt.GridLayout;
038:
039: import java.util.Arrays;
040:
041: import javax.swing.Action;
042: import javax.swing.InputMap;
043: import javax.swing.JMenuItem;
044: import javax.swing.JPanel;
045: import javax.swing.JPopupMenu;
046: import javax.swing.JScrollPane;
047: import javax.swing.JSplitPane;
048: import javax.swing.KeyStroke;
049: import javax.swing.border.EmptyBorder;
050: import javax.swing.event.TreeSelectionEvent;
051: import javax.swing.event.TreeSelectionListener;
052: import javax.swing.tree.TreePath;
053:
054: /**
055: * The panel which visualizes the tree record and the editor.
056: *
057: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
058: * @version 1.02
059: */
060: public class RecordPanel extends JPanel implements
061: TreeSelectionListener {
062: /** The CleverPHL instance. */
063: private CleverPHL cleverPHL;
064:
065: /** The environment. */
066: private Environment env;
067:
068: /** The structure element panel. */
069: private StructureElementPanel elementPanel;
070:
071: /** The structure tree. */
072: private StructureTree tree;
073:
074: /**
075: * Creates a new panel
076: *
077: * @param cleverPHL the CleverPHL instance
078: * @param session session whose tree record should be visualized
079: */
080: public RecordPanel(CleverPHL cleverPHL, Session session) {
081: super ();
082: this .cleverPHL = cleverPHL;
083: this .env = cleverPHL.getEnvironment();
084:
085: tree = new StructureTree(env, session.getStructure());
086: session.setStructureTree(tree);
087:
088: JScrollPane recordScrollPane = new JScrollPane(tree,
089: JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
090: JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
091: recordScrollPane.setBackground(Color.white);
092: recordScrollPane.setBorder(new EmptyBorder(5, 5, 5, 5));
093:
094: elementPanel = new StructureElementPanel(cleverPHL, session,
095: tree);
096:
097: JPanel right = new JPanel();
098: right.setLayout(new FlowLayout(FlowLayout.LEFT));
099: right.add(elementPanel);
100:
101: JScrollPane elementScrollPane = new JScrollPane(right,
102: JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
103: JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
104: elementScrollPane.setBorder(new EmptyBorder(5, 5, 5, 5));
105:
106: JPopupMenu treePopup = (JPopupMenu) cleverPHL.getMainFrame()
107: .getCleverPHLMenuBar()
108: .getMenuComponentFromCustomization(
109: "CleverPHL.Menu.RecordTreePopup");
110: tree.addMouseListener(new PopupListener(treePopup));
111:
112: // Add key shortcuts to the tree
113: Component[] popupMenuItems = treePopup.getComponents();
114: InputMap inputMap = tree.getInputMap();
115:
116: for (int i = 0; i < popupMenuItems.length; i++) {
117: if (popupMenuItems[i] instanceof JMenuItem) {
118: JMenuItem item = (JMenuItem) popupMenuItems[i];
119: Action action = item.getAction();
120: KeyStroke keyStroke = (KeyStroke) action
121: .getValue(Action.ACCELERATOR_KEY);
122:
123: if (keyStroke != null) {
124: inputMap.put(keyStroke, action);
125: }
126: }
127: }
128:
129: //tree.addMouseListener (new RightClickListener ());
130: tree.getSelectionModel().addTreeSelectionListener(this );
131:
132: setLayout(new GridLayout(1, 1));
133:
134: //add (componentsScrollPane);
135: //add (recordScrollPane);
136: //add (elementScrollPane);
137: JSplitPane splitPane = new JSplitPane(
138: JSplitPane.HORIZONTAL_SPLIT, recordScrollPane,
139: elementScrollPane);
140: add(splitPane);
141: }
142:
143: /**
144: * Returns the structure element panel.
145: *
146: * @return DOCUMENT ME!
147: */
148: public StructureElementPanel getStructureElementPanel() {
149: return elementPanel;
150: }
151:
152: /*class RightClickListener extends MouseAdapter {
153: public void mousePressed (MouseEvent event) {
154: if (event.getButton () == MouseEvent.BUTTON3) {
155: System.out.println ("yes");
156: }
157: }
158: }*/
159:
160: /**
161: * Called when the tree selection has changed.
162: *
163: * @param event the tree selection event
164: */
165: public void valueChanged(TreeSelectionEvent event) {
166: updateMenuItems();
167: }
168:
169: /**
170: * Updates the menu items related to this record panel.
171: */
172: public void updateMenuItems() {
173: updateMenuItems(!tree.isSelectionEmpty());
174: }
175:
176: /**
177: * Updates the menu items.
178: *
179: * @param isEnabled whether or not the menu items should be enabled
180: */
181: private void updateMenuItems(boolean isEnabled) {
182: CleverPHLMainFrame mainFrame = cleverPHL.getMainFrame();
183:
184: // Tests whether or not the root element is selected
185: boolean rootElementSelected = false;
186: TreePath[] selectedPaths = tree.getSelectionPaths();
187:
188: if (selectedPaths != null) {
189: for (int i = 0; i < selectedPaths.length; i++) {
190: StructureElement element = (StructureElement) selectedPaths[i]
191: .getLastPathComponent();
192:
193: if (element instanceof RootElement) {
194: rootElementSelected = true;
195: }
196: }
197: }
198:
199: mainFrame.getCleverPHLMenuBar().setMenuItemEnabled(
200: "CleverPHL.Menu.RemoveSelectedRecordables",
201: isEnabled && !rootElementSelected);
202: mainFrame.getCleverPHLMenuBar().setMenuItemEnabled(
203: "CleverPHL.Menu.CutRecordables",
204: isEnabled && !rootElementSelected);
205: mainFrame.getCleverPHLMenuBar().setMenuItemEnabled(
206: "CleverPHL.Menu.CopyRecordables",
207: isEnabled && !rootElementSelected);
208: mainFrame.getCleverPHLMenuBar().setMenuItemEnabled(
209: "CleverPHL.Menu.RecordInsert", isEnabled);
210:
211: // Are the selected rows in a sequence?
212: boolean isSequence = true;
213: int[] selectedRows = tree.getSelectionRows();
214:
215: if (selectedRows != null) {
216: Arrays.sort(selectedRows);
217:
218: for (int i = 0; i < (selectedRows.length - 1); i++) {
219: if ((selectedRows[i] + 1) != selectedRows[i + 1]) {
220: isSequence = false;
221:
222: break;
223: }
224: }
225: }
226:
227: mainFrame.getCleverPHLMenuBar().setMenuItemEnabled(
228: "CleverPHL.Menu.GroupStructureElements",
229: isEnabled && isSequence && !rootElementSelected);
230: mainFrame.getCleverPHLMenuBar().setMenuItemEnabled(
231: "CleverPHL.Menu.InsertRecordables",
232: !cleverPHL.getClipBoard().isEmpty());
233: }
234: }
|