001: // FrameBrowser.java
002: // $Id: FrameBrowser.java,v 1.18 2000/08/16 21:37:30 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1998.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigadmin.editors;
007:
008: import javax.swing.JOptionPane;
009: import javax.swing.JPanel;
010: import javax.swing.BorderFactory;
011: import javax.swing.JLabel;
012: import javax.swing.JButton;
013: import javax.swing.JPopupMenu;
014: import javax.swing.JMenuItem;
015: import javax.swing.tree.TreePath;
016: import javax.swing.tree.DefaultTreeModel;
017: import javax.swing.tree.MutableTreeNode;
018: import javax.swing.plaf.basic.BasicTreeUI;
019:
020: import java.awt.GridBagLayout;
021: import java.awt.GridBagConstraints;
022: import java.awt.Insets;
023: import java.awt.GridLayout;
024: import java.awt.event.ActionListener;
025: import java.awt.event.ActionEvent;
026: import java.awt.dnd.DropTargetDropEvent;
027: import java.awt.dnd.DnDConstants;
028:
029: import java.util.Vector;
030: import java.util.Hashtable;
031: import java.util.Enumeration;
032:
033: import org.w3c.jigadmin.RemoteResourceWrapper;
034: import org.w3c.jigadmin.PropertyManager;
035: import org.w3c.jigadmin.widgets.EditableStringChoice;
036: import org.w3c.jigadmin.widgets.Icons;
037: import org.w3c.jigadmin.gui.Message;
038:
039: import org.w3c.jigsaw.admin.RemoteAccessException;
040: import org.w3c.jigsaw.admin.RemoteResource;
041:
042: import org.w3c.tools.sorter.Sorter;
043:
044: /**
045: * A JTree used to manage frames.
046: * @version $Revision: 1.18 $
047: * @author Benoît Mahé (bmahe@w3.org)
048: */
049: public class FrameBrowser extends ResourceTreeBrowser {
050:
051: /**
052: * Constructor
053: * @param root The resource node
054: */
055: protected FrameBrowser(RemoteResourceWrapperNode root) {
056: super (root);
057: setUI(new BasicTreeUI());
058: }
059:
060: /**
061: * Get a FrameBrowser.
062: * @param rrw The resource
063: * @param name The resource identifier.
064: * @return a FrameBrowser instance
065: */
066: public static FrameBrowser getFrameBrowser(
067: RemoteResourceWrapper rrw, String name) {
068: RemoteFrameWrapperNode rnode = new RemoteFrameWrapperNode(rrw,
069: name);
070: FrameBrowser fb = new FrameBrowser(rnode);
071: return fb;
072: }
073:
074: /**
075: * Drop a frame.
076: * @param cell The resource cell
077: * @see org.w3c.jigadmin.editors.ResourceCell
078: */
079: protected boolean dropResource(ResourceCell cell) {
080: RemoteResourceWrapper rrw = getSelectedResourceWrapper();
081: if (cell.isFrame() || cell.isFilter()) {
082: try {
083: TreePath path = getSelectionPath();
084: addFrame(cell.toString(), rrw, path);
085: return true;
086: } catch (RemoteAccessException ex) {
087: Message.showErrorMessage(this , ex);
088: return false;
089: }
090: } else {
091: return false;
092: }
093: }
094:
095: /**
096: * Get the Panel used to add a new frame
097: * @param title The title
098: * @param rrw The wrapper of the father RemoteResource
099: * @return a AddResourcePanel instance
100: * @see org.w3c.jigadmin.editors.AddResourcePanel
101: */
102: protected AddResourcePanel getAddResourcePanel(String title,
103: RemoteResourceWrapper rrw) throws RemoteAccessException {
104: return new AddFramePanel(title, rrw, this );
105: }
106:
107: private void performAddResourceToSelectedContainer() {
108: popupAddResourceDialog("Add Frame",
109: getSelectedResourceWrapper());
110: if (resClassname != null) {
111: RemoteResourceWrapper rrw = getSelectedResourceWrapper();
112: try {
113: addFrame(resClassname, rrw, getSelectionPath());
114: } catch (RemoteAccessException ex) {
115: Message.showErrorMessage(this , ex);
116: }
117: }
118: }
119:
120: /**
121: * Add a frame to the selected container.
122: */
123: protected void addResourceToSelectedContainer() {
124: RemoteResourceWrapper selected = getSelectedResourceWrapper();
125: PropertyManager pm = PropertyManager.getPropertyManager();
126: if (selected == null) {
127: JOptionPane.showMessageDialog(this , "No resource selected",
128: "Error", JOptionPane.ERROR_MESSAGE);
129: return;
130: } else if (!pm.isExtensible(selected)) {
131: JOptionPane.showMessageDialog(this ,
132: "The resource selected is not " + "extensible.",
133: "Error", JOptionPane.ERROR_MESSAGE);
134: } else {
135: Thread thread = new Thread() {
136: public void run() {
137: performAddResourceToSelectedContainer();
138: }
139: };
140: thread.start();
141: }
142: }
143:
144: /**
145: * Add a frame to the resource wrapped.
146: * @param classname The new frame class name
147: * @param rrwf the Wrapper of the resource
148: * @param fpath The path of the resource node
149: */
150: protected void addFrame(String classname,
151: RemoteResourceWrapper rrwf, TreePath path)
152: throws RemoteAccessException {
153: RemoteResource newframe = rrwf.getResource().registerFrame(
154: null, classname);
155: RemoteResourceWrapper nrrw = new RemoteResourceWrapper(rrwf,
156: newframe);
157: RemoteFrameWrapperNode parent = (RemoteFrameWrapperNode) path
158: .getLastPathComponent();
159: RemoteFrameWrapperNode child = new RemoteFrameWrapperNode(
160: parent, nrrw);
161: ((DefaultTreeModel) getModel())
162: .insertNodeInto(child, parent, 0);
163: expandPath(path);
164: }
165:
166: /**
167: * Delete the frame wrapped by the given wrapper
168: * @param rrw The RemoteResourceWrapper
169: */
170: protected void deleteResource(RemoteResourceWrapper rrw)
171: throws RemoteAccessException {
172: rrw.getFatherResource().unregisterFrame(rrw.getResource());
173: }
174:
175: /**
176: * Delete the frames associated to the selected nodes.
177: * Display an error message if there is no node selected.
178: */
179: protected void deleteSelectedResources() {
180: TreePath path[] = removeDescendants(getSelectionPaths());
181: if (path == null)
182: return;
183: if (path.length > 0) {
184: int result = JOptionPane.showConfirmDialog(this ,
185: "Delete Selected Frame(s)?", "Delete Frame(s)",
186: JOptionPane.YES_NO_OPTION);
187: if (result == JOptionPane.YES_OPTION) {
188: DefaultTreeModel model = (DefaultTreeModel) getModel();
189: for (int i = 0; i < path.length; i++) {
190: RemoteResourceWrapper rrw = getSelectedResourceWrapper(path[i]);
191: if (rrw == null)
192: continue;
193: if (rrw != rootNode.getResourceWrapper()) {
194: try {
195: deleteResource(rrw);
196: } catch (RemoteAccessException ex) {
197: Message.showErrorMessage(this , ex);
198: continue;
199: }
200: MutableTreeNode node = (MutableTreeNode) path[i]
201: .getLastPathComponent();
202: model.removeNodeFromParent(node);
203: } else {
204: JOptionPane.showMessageDialog(this ,
205: "You can't remove the "
206: + "resource here, please use "
207: + "the resource tree",
208: "Information",
209: JOptionPane.INFORMATION_MESSAGE);
210: }
211: }
212: }
213: }
214: }
215:
216: /**
217: * The popup menu action listener.
218: */
219: ActionListener al = new ActionListener() {
220: public void actionPerformed(ActionEvent evt) {
221: String command = evt.getActionCommand();
222: if (command.equals("del")) {
223: deleteSelectedResources();
224: } else if (command.equals("add")) {
225: addResourceToSelectedContainer();
226: } else if (command.equals("info")) {
227: showReferenceDocumentation();
228: }
229: }
230: };
231:
232: /**
233: * Get the popup menu relative to the selected frame.
234: * @param rrw the wrapper of the resource
235: * @return a JPopupMenu instance
236: */
237: protected JPopupMenu getPopupMenu(RemoteResourceWrapper rrw) {
238: boolean frame = rrw.getResource().isFrame();
239:
240: JPopupMenu popupMenu = new JPopupMenu();
241:
242: JMenuItem menuItem = new JMenuItem("Add frame", Icons.addIcon);
243: menuItem.addActionListener(al);
244: menuItem.setActionCommand("add");
245: popupMenu.add(menuItem);
246:
247: if (frame) {
248: menuItem = new JMenuItem("Delete frame", Icons.deleteIcon);
249: menuItem.addActionListener(al);
250: menuItem.setActionCommand("del");
251: popupMenu.add(menuItem);
252: }
253:
254: popupMenu.addSeparator();
255:
256: menuItem = new JMenuItem("Info", Icons.infoIcon);
257: menuItem.addActionListener(al);
258: menuItem.setActionCommand("info");
259: popupMenu.add(menuItem);
260:
261: return popupMenu;
262: }
263:
264: /**
265: * A double click occured on the node with the given path.
266: * @param path The path where the double click occured.
267: */
268: protected void doubleClick(TreePath path) {
269: //nothing
270: }
271: }
|