001: /*
002: * Gruntspud Copyright (C) 2002 Brett Smith. Written by: Brett Smith
003: * <t_magicthize@users.sourceforge.net> This program is free software; you can
004: * redistribute it and/or modify it under the terms of the GNU Library General
005: * Public License as published by the Free Software Foundation; either version 2
006: * of the License, or (at your option) any later version. This program is
007: * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
008: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
009: * PARTICULAR PURPOSE. See the GNU Library General Public License for more
010: * details. You should have received a copy of the GNU Library General Public
011: * License along with this program; if not, write to the Free Software
012: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
013: */
014: package gruntspud.ui.preferences;
015:
016: import gruntspud.CVSRoot;
017: import gruntspud.Constants;
018: import gruntspud.GruntspudContext;
019: import gruntspud.GruntspudUtil;
020: import gruntspud.connection.ConnectionPlugin;
021: import gruntspud.connection.ConnectionProfile;
022: import gruntspud.ui.*;
023: import gruntspud.ui.AbstractTab;
024: import gruntspud.ui.FileNameTextField;
025: import gruntspud.ui.JNumericTextField;
026: import gruntspud.ui.UIUtil;
027: import gruntspud.ui.XTextField;
028: import java.awt.BorderLayout;
029: import java.awt.CardLayout;
030: import java.awt.Color;
031: import java.awt.Component;
032: import java.awt.Dimension;
033: import java.awt.GridBagConstraints;
034: import java.awt.GridBagLayout;
035: import java.awt.Insets;
036: import java.awt.event.ActionEvent;
037: import java.awt.event.ActionListener;
038: import java.io.IOException;
039: import java.util.Collections;
040: import java.util.Comparator;
041: import java.util.Iterator;
042: import java.util.SortedMap;
043: import java.util.Vector;
044: import javax.swing.BorderFactory;
045: import javax.swing.DefaultComboBoxModel;
046: import javax.swing.DefaultListCellRenderer;
047: import javax.swing.JComboBox;
048: import javax.swing.JLabel;
049: import javax.swing.JList;
050: import javax.swing.JPanel;
051: import javax.swing.SwingUtilities;
052: import javax.swing.UIManager;
053: import javax.swing.event.DocumentEvent;
054: import javax.swing.event.DocumentListener;
055: import plugspud.Plugin;
056: import plugspud.PluginManager;
057:
058: /**
059: * Mini text editor
060: *
061: * @author Brett Smiht @created 26 May 2002
062: */
063: public class ConnectionProfileEditor extends AbstractTab implements
064: ActionListener, DocumentListener {
065: public final static String[] ACCESS_TYPES = {
066: "Default (Global options)", "Read / Write", "Read only" };
067:
068: public final static String[] COMPRESSION_TYPES = {
069: "Default (Global options)", "Enabled", "Disabled" };
070:
071: public final static String[] LINE_ENDING_TYPES = { "Default",
072: "Unix", "Windows", "Ignore" };
073:
074: // Private instance variables
075: private XTextField name;
076:
077: private GruntspudContext context;
078:
079: private JComboBox connectionType;
080:
081: private XTextField user;
082:
083: private XTextField hostName;
084:
085: private FileNameTextField repository;
086:
087: private XTextField webCVSURL;
088:
089: private JNumericTextField port;
090:
091: private JLabel cvsRoot;
092:
093: private ConnectionProfile profile;
094:
095: private CVSRoot currentRoot;
096:
097: private boolean isNew;
098:
099: private Vector types;
100:
101: private JPanel additionalOptionsContainer;
102:
103: private CardLayout additionalOptionsLayout;
104:
105: private JComboBox access;
106:
107: private JComboBox compression;
108:
109: private JComboBox lineEndings;
110:
111: private CharSetComboBox encoding;
112:
113: /**
114: * Constructor
115: *
116: * @param host Description of the Parameter
117: */
118: public ConnectionProfileEditor(GruntspudContext context,
119: ConnectionProfile profile) {
120: this (context, profile, true);
121: }
122:
123: /**
124: * Creates a new ConnectionProfileEditor object.
125: *
126: * @param context DOCUMENT ME!
127: * @param profile DOCUMENT ME!
128: * @param isNew DOCUMENT ME!
129: */
130: public ConnectionProfileEditor(GruntspudContext context,
131: ConnectionProfile profile, boolean isNew) {
132: super ("General", UIUtil
133: .getCachedIcon(Constants.ICON_TOOL_LARGE_CONNECT));
134: this .context = context;
135: this .profile = profile;
136: setTabToolTipText("General connection options.");
137: setLayout(new GridBagLayout());
138: setTabMnemonic('c');
139: types = new Vector();
140: Plugin[] plugins = context.getPluginManager()
141: .getPluginsOfClass(ConnectionPlugin.class);
142: for (int j = 0; j < plugins.length; j++) {
143: types.addElement(plugins[j]);
144: }
145: Collections.sort(types, new ConnectionPluginComparator());
146: additionalOptionsLayout = new CardLayout();
147: additionalOptionsContainer = new JPanel(additionalOptionsLayout);
148: for (Iterator i = types.iterator(); i.hasNext();) {
149: ConnectionPlugin p = (ConnectionPlugin) i.next();
150: AbstractAdditionalOptionsPane o = p.getOptionsComponent();
151: SwingUtilities.updateComponentTreeUI(o);
152: additionalOptionsContainer
153: .add(
154: p.getConnectionType(),
155: (o == null) ? new NoAdditionalConnectionOptionsPane()
156: : o);
157: }
158: additionalOptionsContainer.setBorder(BorderFactory
159: .createTitledBorder("Additional Options"));
160: JPanel p = new JPanel(new GridBagLayout());
161: GridBagConstraints gbc = new GridBagConstraints();
162: gbc.anchor = GridBagConstraints.NORTHWEST;
163: gbc.fill = GridBagConstraints.NONE;
164: gbc.insets = new Insets(2, 2, 2, 2);
165: gbc.weightx = 0.0;
166: UIUtil.jGridBagAdd(p, new JLabel("Name"), gbc,
167: GridBagConstraints.RELATIVE);
168: gbc.weightx = 1.0;
169: UIUtil.jGridBagAdd(p, name = new XTextField(15) {
170: public Dimension getMinimumSize() {
171: return new Dimension(120, super .getMinimumSize().height);
172: }
173: }, gbc, GridBagConstraints.REMAINDER);
174: gbc.weightx = 0.0;
175: UIUtil.jGridBagAdd(p, new JLabel("Type"), gbc,
176: GridBagConstraints.RELATIVE);
177: gbc.weightx = 1.0;
178: UIUtil.jGridBagAdd(p, connectionType = new JComboBox(types) {
179: public Dimension getMinimumSize() {
180: return new Dimension(120, super .getMinimumSize().height);
181: }
182: }, gbc, GridBagConstraints.REMAINDER);
183: if (connectionType.getModel().getSize() > 0) {
184: connectionType.setSelectedIndex(0);
185: }
186: connectionType
187: .setRenderer(new ConnectionPluginComboBoxRenderer());
188: connectionType.addActionListener(this );
189: gbc.weightx = 0.0;
190: UIUtil.jGridBagAdd(p, new JLabel("User "), gbc,
191: GridBagConstraints.RELATIVE);
192: gbc.weightx = 1.0;
193: UIUtil.jGridBagAdd(p, user = new XTextField(15) {
194: public Dimension getMinimumSize() {
195: return new Dimension(120, super .getMinimumSize().height);
196: }
197: }, gbc, GridBagConstraints.REMAINDER);
198: user.getDocument().addDocumentListener(this );
199: gbc.weightx = 0.0;
200: UIUtil.jGridBagAdd(p, new JLabel("Host "), gbc,
201: GridBagConstraints.RELATIVE);
202: gbc.weightx = 1.0;
203: UIUtil.jGridBagAdd(p, hostName = new XTextField(15) {
204: public Dimension getMinimumSize() {
205: return new Dimension(120, super .getMinimumSize().height);
206: }
207: }, gbc, GridBagConstraints.REMAINDER);
208: hostName.getDocument().addDocumentListener(this );
209: gbc.weightx = 0.0;
210: UIUtil.jGridBagAdd(p, new JLabel("Port "), gbc,
211: GridBagConstraints.RELATIVE);
212: gbc.weightx = 1.0;
213: UIUtil.jGridBagAdd(p, port = new JNumericTextField(new Integer(
214: -1), new Integer(65535)) {
215: public Dimension getMinimumSize() {
216: return new Dimension(120, super .getMinimumSize().height);
217: }
218: }, gbc, GridBagConstraints.REMAINDER);
219: port.getDocument().addDocumentListener(this );
220: gbc.weightx = 0.0;
221: UIUtil.jGridBagAdd(p, new JLabel("Repository "), gbc,
222: GridBagConstraints.RELATIVE);
223: gbc.weightx = 1.0;
224: UIUtil.jGridBagAdd(p, repository = new FileNameTextField(null,
225: "", 15, false) {
226: public Dimension getMinimumSize() {
227: return new Dimension(120, super .getMinimumSize().height);
228: }
229: }, gbc, GridBagConstraints.REMAINDER);
230: repository.getDocument().addDocumentListener(this );
231: gbc.weightx = 0.0;
232: UIUtil.jGridBagAdd(p, new JLabel("ViewCVS base URL "), gbc,
233: GridBagConstraints.RELATIVE);
234: gbc.weightx = 1.0;
235: UIUtil.jGridBagAdd(p, webCVSURL = new XTextField(15) {
236: public Dimension getMinimumSize() {
237: return new Dimension(120, super .getMinimumSize().height);
238: }
239: }, gbc, GridBagConstraints.REMAINDER);
240: gbc.weightx = 0.0;
241: UIUtil.jGridBagAdd(p, new JLabel("Access "), gbc,
242: GridBagConstraints.RELATIVE);
243: gbc.weightx = 1.0;
244: UIUtil.jGridBagAdd(p, access = new JComboBox(ACCESS_TYPES) {
245: public Dimension getMinimumSize() {
246: return new Dimension(120, super .getMinimumSize().height);
247: }
248: }, gbc, GridBagConstraints.REMAINDER);
249: gbc.weightx = 0.0;
250: UIUtil.jGridBagAdd(p, new JLabel("Compression "), gbc,
251: GridBagConstraints.RELATIVE);
252: gbc.weightx = 1.0;
253: UIUtil.jGridBagAdd(p, compression = new JComboBox(
254: COMPRESSION_TYPES) {
255: public Dimension getMinimumSize() {
256: return new Dimension(120, super .getMinimumSize().height);
257: }
258: }, gbc, GridBagConstraints.REMAINDER);
259: gbc.weightx = 0.0;
260: UIUtil.jGridBagAdd(p, new JLabel("Line endings "), gbc,
261: GridBagConstraints.RELATIVE);
262: gbc.weightx = 1.0;
263: UIUtil.jGridBagAdd(p, lineEndings = new JComboBox(
264: LINE_ENDING_TYPES) {
265: public Dimension getMinimumSize() {
266: return new Dimension(120, super .getMinimumSize().height);
267: }
268: }, gbc, GridBagConstraints.REMAINDER);
269: gbc.weighty = 1.0;
270: gbc.weightx = 0.0;
271: UIUtil.jGridBagAdd(p, new JLabel("Encoding "), gbc,
272: GridBagConstraints.RELATIVE);
273: gbc.weightx = 1.0;
274: UIUtil.jGridBagAdd(p, encoding = new CharSetComboBox() {
275: public Dimension getMinimumSize() {
276: return new Dimension(120, super .getMinimumSize().height);
277: }
278: }, gbc, GridBagConstraints.REMAINDER);
279: encoding.setEditable(true);
280: cvsRoot = new JLabel(" ");
281: cvsRoot.setHorizontalAlignment(JLabel.CENTER);
282: cvsRoot.setForeground(Color.red);
283: cvsRoot
284: .setFont(UIManager.getFont("Label.font")
285: .deriveFont(10f));
286: cvsRoot.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
287: setLayout(new BorderLayout());
288: add(p, BorderLayout.CENTER);
289: add(additionalOptionsContainer, BorderLayout.EAST);
290: add(cvsRoot, BorderLayout.SOUTH);
291: setConnectionProfile(profile, isNew);
292: }
293:
294: /**
295: * DOCUMENT ME!
296: */
297: public void tabSelected() {
298: }
299:
300: /**
301: * DOCUMENT ME!
302: */
303: public void applyTab() {
304: if ((currentRoot != null) && (profile != null)) {
305: if (profile.getCVSRoot() == null) {
306: profile.setCVSRoot(currentRoot);
307: }
308: profile.setName(name.getText());
309: profile.getCVSRoot().setConnectionType(
310: currentRoot.getConnectionType());
311: profile.getCVSRoot().setUser(currentRoot.getUser());
312: profile.getCVSRoot().setHost(currentRoot.getHost());
313: profile.getCVSRoot().setPort(currentRoot.getPort());
314: profile.getCVSRoot().setRepository(
315: currentRoot.getRepository());
316: profile.setWebCVSURL(webCVSURL.getText());
317: profile.setAccess((access.getSelectedIndex() == -1) ? 0
318: : access.getSelectedIndex());
319: profile
320: .setCompression((compression.getSelectedIndex() == -1) ? 0
321: : compression.getSelectedIndex());
322: profile
323: .setLineEndings((lineEndings.getSelectedIndex() == -1) ? 0
324: : lineEndings.getSelectedIndex());
325: profile.setEncoding(encoding.getSelectedEncoding());
326: int i = connectionType.getSelectedIndex();
327: ((ConnectionPlugin) types.elementAt(i))
328: .getOptionsComponent().applyOptions();
329: }
330: }
331:
332: /**
333: * DOCUMENT ME!
334: *
335: * @return DOCUMENT ME!
336: */
337: public boolean validateTab() {
338: ConnectionPlugin t = (ConnectionPlugin) connectionType
339: .getSelectedItem();
340: try {
341: if (name.getText().equals("")) {
342: throw new IOException(
343: "You must give the profile a unique name");
344: }
345: if (t == null) {
346: throw new IOException(
347: "You must select a connection type");
348: }
349: if (repository.getText().equals("")) {
350: throw new IOException(
351: "You must specify a repository path");
352: }
353: t.validateCVSRoot(currentRoot);
354: } catch (Exception e) {
355: GruntspudUtil.showErrorMessage(this , "Connection Details",
356: e);
357: return false;
358: }
359: if (!t.getOptionsComponent().validateOptions()) {
360: return false;
361: }
362: return true;
363: }
364:
365: /**
366: * DOCUMENT ME!
367: *
368: * @param profile DOCUMENT ME!
369: */
370: public void setConnectionProfile(ConnectionProfile profile) {
371: setConnectionProfile(profile, true);
372: }
373:
374: private void showSelectedAdditional() {
375: if (connectionType.getSelectedItem() != null) {
376: additionalOptionsLayout.show(additionalOptionsContainer,
377: ((ConnectionPlugin) connectionType
378: .getSelectedItem()).getConnectionType());
379: }
380: }
381:
382: /**
383: * DOCUMENT ME!
384: *
385: * @param profile DOCUMENT ME!
386: * @param isNew DOCUMENT ME!
387: */
388: public void setConnectionProfile(ConnectionProfile profile,
389: boolean isNew) {
390: String t = ((profile == null) || (profile.getCVSRoot() == null)) ? null
391: : profile.getCVSRoot().getConnectionType();
392: boolean found = false;
393: for (int i = 0; (i < types.size()) && !found; i++) {
394: ConnectionPlugin w = (ConnectionPlugin) types.elementAt(i);
395: if (w.getConnectionType().equals(t)) {
396: connectionType.setSelectedIndex(i);
397: found = true;
398: }
399: }
400: if (!found) {
401: for (int i = 0; i < connectionType.getModel().getSize(); i++) {
402: if (((ConnectionPlugin) connectionType.getModel()
403: .getElementAt(i)).getConnectionType().equals(
404: "local")) {
405: connectionType.setSelectedIndex(i);
406: break;
407: }
408: }
409: }
410: encoding.setSelectedEncoding(profile.getEncoding());
411: name.setText((profile == null) ? "" : profile.getName());
412: user.setText(((profile == null)
413: || (profile.getCVSRoot() == null) || (profile
414: .getCVSRoot().getUser() == null)) ? "" : profile
415: .getCVSRoot().getUser());
416: hostName.setText(((profile == null)
417: || (profile.getCVSRoot() == null) || (profile
418: .getCVSRoot().getHost() == null)) ? "" : profile
419: .getCVSRoot().getHost());
420: port.setValue(new Integer(((profile == null) || (profile
421: .getCVSRoot() == null)) ? (-1) : profile.getCVSRoot()
422: .getPort()));
423: repository.setText(((profile == null)
424: || (profile.getCVSRoot() == null) || (profile
425: .getCVSRoot().getRepository() == null)) ? "" : profile
426: .getCVSRoot().getRepository());
427: webCVSURL
428: .setText(((profile == null) || (profile.getWebCVSURL() == null)) ? ""
429: : profile.getWebCVSURL());
430: access.setSelectedIndex((profile == null) ? (-1) : profile
431: .getAccess());
432: compression.setSelectedIndex((profile == null) ? (-1) : profile
433: .getCompression());
434: lineEndings.setSelectedIndex((profile == null) ? (-1) : profile
435: .getLineEndings());
436: this .isNew = isNew;
437: for (int i = 0; i < types.size(); i++) {
438: ((ConnectionPlugin) types.elementAt(i))
439: .getOptionsComponent().setProfile(profile);
440: }
441: showSelectedAdditional();
442: setAvailableActions();
443: }
444:
445: /**
446: * DOCUMENT ME!
447: *
448: * @param evt DOCUMENT ME!
449: */
450: public void actionPerformed(ActionEvent evt) {
451: setAvailableActions();
452: showSelectedAdditional();
453: }
454:
455: private void setAvailableActions() {
456: name.setEnabled(isNew);
457: ConnectionPlugin w = ((ConnectionPlugin) connectionType
458: .getSelectedItem());
459: user.setEnabled((w != null) && w.isUserRequired());
460: hostName.setEnabled((w != null) && w.isHostnameRequired());
461: port.setEnabled((w != null) && w.isPortRequired());
462: repository.setAutoComplete(w != null
463: && w.getConnectionType().equals("local"));
464: currentRoot = new CVSRoot();
465: currentRoot.setConnectionType((w == null) ? null : w
466: .getConnectionType());
467: currentRoot.setUser((user.getText().length() == 0) ? null
468: : user.getText());
469: currentRoot.setHost((hostName.getText().length() == 0) ? null
470: : hostName.getText());
471: currentRoot
472: .setRepository((repository.getText().length() == 0) ? null
473: : repository.getText());
474: int val = ((Integer) port.getValue()).intValue();
475: currentRoot.setPort((val == 0) ? (-1) : val);
476: String r = currentRoot.toString();
477: cvsRoot.setText(r.length() == 0 ? " " : r);
478: cvsRoot.setToolTipText(r);
479: }
480:
481: /**
482: * DOCUMENT ME!
483: *
484: * @param e DOCUMENT ME!
485: */
486: public void insertUpdate(DocumentEvent e) {
487: setAvailableActions();
488: }
489:
490: /**
491: * DOCUMENT ME!
492: *
493: * @param e DOCUMENT ME!
494: */
495: public void removeUpdate(DocumentEvent e) {
496: setAvailableActions();
497: }
498:
499: /**
500: * DOCUMENT ME!
501: *
502: * @param e DOCUMENT ME!
503: */
504: public void changedUpdate(DocumentEvent e) {
505: setAvailableActions();
506: }
507:
508: class ConnectionPluginComboBoxRenderer extends
509: DefaultListCellRenderer {
510: public Component getListCellRendererComponent(JList list,
511: Object value, int index, boolean isSelected,
512: boolean cellHasFocus) {
513: super .getListCellRendererComponent(list, value, index,
514: isSelected, cellHasFocus);
515: ConnectionPlugin p = (ConnectionPlugin) value;
516: setText(((p == null) || (p.getConnectionType() == null)) ? "<No connection type!>"
517: : p.getConnectionType());
518: setIcon((p == null) ? null : p.getIcon());
519: if (p != null) {
520: setToolTipText(context.getPluginManager()
521: .getPluginProperties(p).getProperty(
522: PluginManager.PLUGIN_SHORT_DESCRIPTION));
523: } else {
524: setToolTipText("");
525: }
526: return this ;
527: }
528: }
529:
530: class ConnectionPluginComparator implements Comparator {
531: public boolean equals(Object other) {
532: return (this .equals(other));
533: }
534:
535: public int compare(Object o1, Object o2) {
536: return ((ConnectionPlugin) o1)
537: .getConnectionType()
538: .compareTo(
539: ((ConnectionPlugin) o2).getConnectionType());
540: }
541: }
542: }
|