001: // AddResourcePanel.java
002: // $Id: AddResourcePanel.java,v 1.5 2000/08/16 21:37:30 ylafon Exp $
003: // (c) COPYRIGHT MIT, INRIA and Keio, 1999.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigadmin.editors;
007:
008: import javax.swing.JTextField;
009: import javax.swing.JLabel;
010: import javax.swing.JButton;
011: import javax.swing.JPanel;
012: import javax.swing.BorderFactory;
013:
014: import java.awt.GridLayout;
015: import java.awt.GridBagLayout;
016: import java.awt.GridBagConstraints;
017: import java.awt.Insets;
018: import java.awt.event.ActionListener;
019: import java.awt.event.ActionEvent;
020:
021: import java.util.Vector;
022: import java.util.Hashtable;
023: import java.util.Enumeration;
024:
025: import org.w3c.jigadmin.RemoteResourceWrapper;
026: import org.w3c.jigadmin.PropertyManager;
027: import org.w3c.jigadmin.widgets.EditableStringChoice;
028:
029: import org.w3c.jigsaw.admin.RemoteResource;
030: import org.w3c.jigsaw.admin.RemoteAccessException;
031:
032: import org.w3c.tools.sorter.Sorter;
033:
034: /**
035: * A widget used to select a resource class and its identifier.
036: * @version $Revision: 1.5 $
037: * @author Benoît Mahé (bmahe@w3.org)
038: */
039: public class AddResourcePanel extends JPanel {
040:
041: protected boolean ok;
042:
043: protected EditableStringChoice classSC;
044:
045: protected JTextField identifierTF;
046:
047: protected ResourceTreeBrowser browser = null;
048:
049: /**
050: * Our internal ActionListener
051: */
052: ActionListener al = new ActionListener() {
053: public void actionPerformed(ActionEvent ae) {
054: if (ae.getActionCommand().equals("Ok")
055: || ae.getSource().equals(identifierTF)) {
056: if (!classSC.getText().equals("")) {
057: if (!identifierTF.getText().equals("")) {
058: browser.setResourceToAdd(classSC.getText(),
059: identifierTF.getText());
060: browser.disposeAddResourcePopup();
061: done();
062: } else {
063: identifierTF.requestFocus();
064: }
065: } else {
066: classSC.requestFocus();
067: }
068: } else if (ae.getActionCommand().equals("Cancel")) {
069: browser.setResourceToAdd(null, null);
070: browser.disposeAddResourcePopup();
071: done();
072: } else if (ae.getSource().equals(classSC)) {
073: identifierTF.requestFocus();
074: }
075: }
076: };
077:
078: /**
079: * Get a list of resources that we can add to the container wrapped by
080: * the given RemoteResourceWrapper.
081: * @param rrw The RemoteResourceWrapper
082: * @return a Hashtable instance containing the list of resource that can
083: * be added to the given RemoteResource.
084: * @exception RemoteAccessException is some remote error occurs
085: */
086: protected Hashtable getResources(RemoteResourceWrapper rrw)
087: throws RemoteAccessException {
088: PropertyManager pm = PropertyManager.getPropertyManager();
089: RemoteResource rr = rrw.getResource();
090: if (rr.isIndexersCatalog())
091: return pm.getIndexers();
092: else
093: return pm.getResources();
094: }
095:
096: /**
097: * Initialize the StringChoice.
098: * @param rrw The RemoteResourceWrapper of the container where we are going
099: * to add a resource.
100: * @exception RemoteAccessException is some remote error occurs
101: */
102: protected void initializeStringChoice(RemoteResourceWrapper rrw)
103: throws RemoteAccessException {
104: classSC = new EditableStringChoice();
105: Hashtable resources = getResources(rrw);
106: Enumeration e = (Sorter.sortStringEnumeration(resources.keys()))
107: .elements();
108: Vector cells = new Vector(10);
109: while (e.hasMoreElements()) {
110: String name = (String) e.nextElement();
111: ResourceCell cell = new ResourceCell(name,
112: (String) resources.get(name));
113: cells.addElement(cell);
114: }
115:
116: Object items[] = new Object[cells.size()];
117: cells.copyInto(items);
118: classSC.initialize(items);
119: classSC.setRenderer(new ResourceCellRenderer());
120: classSC.setMaximumRowCount(5);
121: }
122:
123: /**
124: * Build the interface.
125: * @param rrw The RemoteResourceWrapper of the container where we are going
126: * to add a resource.
127: * @param title the title of the panel.
128: * @exception RemoteAccessException is some remote error occurs
129: */
130: protected void build(RemoteResourceWrapper rrw, String title)
131: throws RemoteAccessException {
132: GridBagLayout gbl = new GridBagLayout();
133: GridBagConstraints gbc = new GridBagConstraints();
134: GridBagLayout mgbl = new GridBagLayout();
135: GridBagConstraints mgbc = new GridBagConstraints();
136: JLabel l;
137: JButton b;
138: JPanel p = new JPanel(gbl);
139:
140: initializeStringChoice(rrw);
141:
142: identifierTF = new JTextField(25);
143: identifierTF.addActionListener(al);
144: identifierTF
145: .setBorder(BorderFactory.createLoweredBevelBorder());
146:
147: gbc.fill = GridBagConstraints.HORIZONTAL;
148: gbc.weightx = 0;
149: gbc.weighty = 0;
150: gbc.insets = new Insets(0, 0, 10, 0);
151: mgbc.fill = GridBagConstraints.NONE;
152: mgbc.weightx = 0;
153: mgbc.weighty = 0;
154: mgbc.insets = new Insets(0, 10, 16, 5);
155: setLayout(mgbl);
156:
157: l = new JLabel("Class name: ", JLabel.RIGHT);
158: gbc.gridwidth = 1;
159: gbl.setConstraints(l, gbc);
160: p.add(l);
161: gbc.gridwidth = GridBagConstraints.REMAINDER;
162: gbl.setConstraints(classSC, gbc);
163: p.add(classSC);
164:
165: l = new JLabel("Identifier: ", JLabel.RIGHT);
166: gbc.gridwidth = 1;
167: gbl.setConstraints(l, gbc);
168: p.add(l);
169: gbc.gridwidth = GridBagConstraints.REMAINDER;
170: gbl.setConstraints(identifierTF, gbc);
171: p.add(identifierTF);
172: mgbc.gridwidth = GridBagConstraints.REMAINDER;
173: mgbl.setConstraints(p, mgbc);
174: add(p);
175:
176: // and now the usual button bar
177: p = new JPanel(new GridLayout(1, 2, 20, 20));
178: b = new JButton("Ok");
179: b.addActionListener(al);
180: p.add(b);
181: b = new JButton("Cancel");
182: b.addActionListener(al);
183: p.add(b);
184: mgbl.setConstraints(p, mgbc);
185: add(p);
186: setBorder(BorderFactory.createTitledBorder(title));
187: }
188:
189: /**
190: * Constructor.
191: * @param title The widget title
192: * @param rrw The RemoteResourceWrapper of the container where we are
193: * going to add a resource.
194: * @param browser the ResourceTreeBrowser
195: * @exception RemoteAccessException is some remote error occurs
196: */
197: protected AddResourcePanel(String title, RemoteResourceWrapper rrw,
198: ResourceTreeBrowser browser) throws RemoteAccessException {
199: this .ok = false;
200: this .browser = browser;
201: build(rrw, title);
202: }
203:
204: /**
205: * request the focus for the class combobox.
206: */
207: protected void getFocus() {
208: classSC.requestFocus();
209: }
210:
211: /**
212: * NotifyAll that the resource class and its identifier are selected.
213: */
214: protected synchronized void done() {
215: ok = true;
216: notifyAll();
217: }
218:
219: /**
220: * Wait until the resource class and its identifier are selected.
221: */
222: public synchronized boolean waitForCompletion() {
223: try {
224: wait();
225: } catch (InterruptedException ex) {
226: //nothing to do
227: }
228: return ok;
229: }
230:
231: }
|