001: /*
002: * Gruntspud
003: *
004: * Copyright (C) 2002 Brett Smith.
005: *
006: * Written by: Brett Smith <t_magicthize@users.sourceforge.net>
007: *
008: * This program is free software; you can redistribute it and/or modify it under
009: * the terms of the GNU Library General Public License as published by the Free
010: * Software Foundation; either version 2 of the License, or (at your option) any
011: * later version. This program is distributed in the hope that it will be
012: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
014: * General Public License for more details.
015: *
016: * You should have received a copy of the GNU Library General Public License
017: * along with this program; if not, write to the Free Software Foundation, Inc.,
018: * 675 Mass Ave, Cambridge, MA 02139, USA.
019: */
020: package gruntspud.connection;
021:
022: import gruntspud.CVSRoot;
023: import gruntspud.CVSUtil;
024: import gruntspud.Constants;
025: import gruntspud.GruntspudContext;
026: import gruntspud.SortCriteria;
027: import java.io.File;
028: import java.util.Collections;
029: import java.util.Comparator;
030: import java.util.Enumeration;
031: import java.util.HashMap;
032: import java.util.Properties;
033: import java.util.ResourceBundle;
034: import java.util.Vector;
035: import javax.swing.table.AbstractTableModel;
036:
037: /**
038: * Description of the Class
039: *
040: *@author magicthize
041: *@created 26 May 2002
042: */
043: public class ConnectionProfileModel extends AbstractTableModel {
044: // Private instance variables
045: static ResourceBundle res = ResourceBundle
046: .getBundle("gruntspud.connection.ResourceBundle");
047:
048: private Vector profiles;
049:
050: private ConnectionProfile defaultProfile;
051:
052: private GruntspudContext context;
053:
054: private SortCriteria sortCriteria;
055:
056: /**
057: * Constructor
058: */
059: public ConnectionProfileModel(GruntspudContext context) {
060: profiles = new Vector();
061: this .context = context;
062: //
063: sortCriteria = new SortCriteria(context.getHost()
064: .getIntegerProperty(
065: Constants.CONNECTION_PROFILE_TABLE_SORT_COLUMN,
066: 0), context.getHost().getIntegerProperty(
067: Constants.CONNECTION_PROFILE_TABLE_SORT_DIRECTION,
068: SortCriteria.SORT_ASCENDING), false, true);
069: Properties p = context.getHost().getProperties();
070: try {
071: HashMap map = new HashMap();
072: for (Enumeration e = p.keys(); e.hasMoreElements();) {
073: String n = (String) e.nextElement();
074: if (n.startsWith(Constants.CONNECTION_PROFILE_PREFIX)) {
075: int idx = n.indexOf('.',
076: Constants.CONNECTION_PROFILE_PREFIX
077: .length());
078: String number = n.substring(
079: Constants.CONNECTION_PROFILE_PREFIX
080: .length(), idx);
081: String key = n
082: .substring(Constants.CONNECTION_PROFILE_PREFIX
083: .length()
084: + number.length());
085: String val = (String) p.getProperty(n);
086: ConnectionProfile profile = (ConnectionProfile) map
087: .get(number);
088: if (profile == null) {
089: profile = new ConnectionProfile();
090: profile.setCVSRoot(new CVSRoot());
091: map.put(number, profile);
092: addProfile(profile, false);
093: }
094: if (key
095: .equals(Constants.CONNECTION_PROFILE_NAME_SUFFIX)) {
096: profile.setName(val);
097: } else if (key
098: .equals(Constants.CONNECTION_PROFILE_TYPE_SUFFIX)) {
099: profile.getCVSRoot().setConnectionType(val);
100: } else if (key
101: .equals(Constants.CONNECTION_PROFILE_USER_SUFFIX)) {
102: profile.getCVSRoot().setUser(val);
103: } else if (key
104: .equals(Constants.CONNECTION_PROFILE_HOST_SUFFIX)) {
105: profile.getCVSRoot().setHost(val);
106: } else if (key
107: .equals(Constants.CONNECTION_PROFILE_REPOSITORY_SUFFIX)) {
108: profile.getCVSRoot().setRepository(val);
109: } else if (key
110: .equals(Constants.CONNECTION_PROFILE_PORT_SUFFIX)) {
111: profile.getCVSRoot().setPort(
112: Integer.parseInt(val));
113: } else if (key
114: .equals(Constants.CONNECTION_PROFILE_DEFAULT_SUFFIX)) {
115: setDefaultProfile(profile);
116: } else if (key
117: .equals(Constants.CONNECTION_PROFILE_ACCESS_SUFFIX)) {
118: profile.setAccess(Integer.parseInt(val));
119: } else if (key
120: .equals(Constants.CONNECTION_PROFILE_COMPRESSION_SUFFIX)) {
121: profile.setCompression(Integer.parseInt(val));
122: } else if (key
123: .equals(Constants.CONNECTION_PROFILE_LINE_ENDINGS_SUFFIX)) {
124: profile.setLineEndings(Integer.parseInt(val));
125: } else if (key
126: .equals(Constants.CONNECTION_PROFILE_WEB_CVS_URL_SUFFIX)) {
127: profile.setWebCVSURL(val);
128: } else if (key
129: .equals(Constants.CONNECTION_PROFILE_ENCODING_SUFFIX)) {
130: profile
131: .setEncoding(val.equals("") ? null
132: : val);
133: } else {
134: profile.setProperty(key.substring(1), val);
135: }
136: }
137: }
138: } catch (NumberFormatException nfe) {
139: nfe.printStackTrace();
140: }
141: resort();
142: }
143:
144: /**
145: * DOCUMENT ME!
146: */
147: public void resort() {
148: Collections.sort(profiles, new ConnectionProfileComparator());
149: }
150:
151: /**
152: * DOCUMENT ME!
153: *
154: * @return DOCUMENT ME!
155: */
156: public SortCriteria getSortCriteria() {
157: return sortCriteria;
158: }
159:
160: /**
161: * DOCUMENT ME!
162: *
163: * @param root DOCUMENT ME!
164: *
165: * @return DOCUMENT ME!
166: */
167: public ConnectionProfile getProfileForCVSRoot(String root) {
168: CVSRoot r = new CVSRoot(root);
169: return getProfileForCVSRoot(r);
170: }
171:
172: /**
173: * DOCUMENT ME!
174: *
175: * @param r DOCUMENT ME!
176: *
177: * @return DOCUMENT ME!
178: */
179: public ConnectionProfile getProfileForCVSRoot(CVSRoot r) {
180: if (r != null) {
181: for (int i = 0; i < getRowCount(); i++) {
182: /** @todo maybe this needs to be better */
183: boolean ignoreRepositoryCase = (r.getRepository()
184: .indexOf('\\') != -1)
185: || (new File(r.getRepository()).isAbsolute()
186: && (r.getRepository().length() > 2)
187: && (r.getRepository().charAt(1) == ':') && ((r
188: .getRepository().charAt(2) == '/') || (r
189: .getRepository().charAt(2) == '\\')));
190: ConnectionProfile p = getConnectionProfileAt(i);
191: String alias = CVSUtil.getAliasForConnectionType(r
192: .getConnectionType());
193: if ((p.getCVSRoot().getConnectionType().equals(
194: r.getConnectionType()) || p.getCVSRoot()
195: .getConnectionType().equals(alias))
196: && ((r.getUser() == null) || (r.getUser()
197: .equals(p.getCVSRoot().getUser())))
198: && ((r.getHost() == null) || (r.getHost()
199: .equalsIgnoreCase(p.getCVSRoot()
200: .getHost())))
201: && ((r.getRepository() == null) || (ignoreRepositoryCase ? r
202: .getRepository().equalsIgnoreCase(
203: p.getCVSRoot().getRepository())
204: : r.getRepository().equals(
205: p.getCVSRoot().getRepository())))) {
206: return p;
207: }
208: }
209: }
210: return null;
211: }
212:
213: /**
214: * Save the current model
215: */
216: public void apply() {
217: Properties p = context.getHost().getProperties();
218: for (Enumeration e = p.keys(); e.hasMoreElements();) {
219: String n = (String) e.nextElement();
220: if (n.startsWith(Constants.CONNECTION_PROFILE_PREFIX)) {
221: p.remove(n);
222: }
223: }
224: for (int i = 0; i < getRowCount(); i++) {
225: ConnectionProfile profile = getConnectionProfileAt(i);
226: context.getHost().setProperty(
227: Constants.CONNECTION_PROFILE_PREFIX + i
228: + Constants.CONNECTION_PROFILE_TYPE_SUFFIX,
229: profile.getCVSRoot().getConnectionType());
230: if (profile.getName() != null) {
231: context
232: .getHost()
233: .setProperty(
234: Constants.CONNECTION_PROFILE_PREFIX
235: + i
236: + Constants.CONNECTION_PROFILE_NAME_SUFFIX,
237: profile.getName());
238: }
239: if (profile.getCVSRoot().getUser() != null) {
240: context
241: .getHost()
242: .setProperty(
243: Constants.CONNECTION_PROFILE_PREFIX
244: + i
245: + Constants.CONNECTION_PROFILE_USER_SUFFIX,
246: profile.getCVSRoot().getUser());
247: }
248: if (profile.getCVSRoot().getHost() != null) {
249: context
250: .getHost()
251: .setProperty(
252: Constants.CONNECTION_PROFILE_PREFIX
253: + i
254: + Constants.CONNECTION_PROFILE_HOST_SUFFIX,
255: profile.getCVSRoot().getHost());
256: }
257: if (profile.getCVSRoot().getRepository() != null) {
258: context
259: .getHost()
260: .setProperty(
261: Constants.CONNECTION_PROFILE_PREFIX
262: + i
263: + Constants.CONNECTION_PROFILE_REPOSITORY_SUFFIX,
264: profile.getCVSRoot().getRepository());
265: }
266: if (profile.getCVSRoot().getPort() != -1) {
267: context
268: .getHost()
269: .setProperty(
270: Constants.CONNECTION_PROFILE_PREFIX
271: + i
272: + Constants.CONNECTION_PROFILE_PORT_SUFFIX,
273: String.valueOf(profile.getCVSRoot()
274: .getPort()));
275: }
276: context
277: .getHost()
278: .setProperty(
279: Constants.CONNECTION_PROFILE_PREFIX
280: + i
281: + Constants.CONNECTION_PROFILE_ACCESS_SUFFIX,
282: String.valueOf(profile.getAccess()));
283: context
284: .getHost()
285: .setProperty(
286: Constants.CONNECTION_PROFILE_PREFIX
287: + i
288: + Constants.CONNECTION_PROFILE_COMPRESSION_SUFFIX,
289: String.valueOf(profile.getCompression()));
290: context
291: .getHost()
292: .setProperty(
293: Constants.CONNECTION_PROFILE_PREFIX
294: + i
295: + Constants.CONNECTION_PROFILE_LINE_ENDINGS_SUFFIX,
296: String.valueOf(profile.getLineEndings()));
297: context
298: .getHost()
299: .setProperty(
300: Constants.CONNECTION_PROFILE_PREFIX
301: + i
302: + Constants.CONNECTION_PROFILE_ENCODING_SUFFIX,
303: profile.getEncoding() == null ? ""
304: : profile.getEncoding());
305: context
306: .getHost()
307: .setProperty(
308: Constants.CONNECTION_PROFILE_PREFIX
309: + i
310: + Constants.CONNECTION_PROFILE_WEB_CVS_URL_SUFFIX,
311: profile.getWebCVSURL());
312: if (profile == getDefaultProfile()) {
313: context
314: .getHost()
315: .setProperty(
316: Constants.CONNECTION_PROFILE_PREFIX
317: + i
318: + Constants.CONNECTION_PROFILE_DEFAULT_SUFFIX,
319: "true");
320: }
321: for (Enumeration e = profile.keys(); e.hasMoreElements();) {
322: String key = (String) e.nextElement();
323: context.getHost().setProperty(
324: Constants.CONNECTION_PROFILE_PREFIX + i + "."
325: + key, profile.getProperty(key));
326: }
327: }
328: context.getHost().setIntegerProperty(
329: Constants.CONNECTION_PROFILE_TABLE_SORT_COLUMN,
330: sortCriteria.getSortType());
331: context.getHost().setIntegerProperty(
332: Constants.CONNECTION_PROFILE_TABLE_SORT_DIRECTION,
333: sortCriteria.getSortDirection());
334: }
335:
336: /**
337: * Return the <code>ConnectionProfile</code> at the specified index
338: *
339: * @param
340: * @return profile
341: */
342: public ConnectionProfile getConnectionProfileAt(int r) {
343: return (ConnectionProfile) profiles.elementAt(r);
344: }
345:
346: /**
347: * Gets number of connections
348: *
349: * @return The rowCount value
350: */
351: public int getRowCount() {
352: return profiles.size();
353: }
354:
355: /**
356: * Gets the columnCount attribute of the CVSFileNodeTableModel object
357: *
358: * @return The columnCount value
359: */
360: public int getColumnCount() {
361: return 5;
362: }
363:
364: /**
365: * Gets the columnName attribute of the CVSFileNodeTableModel object
366: *
367: * @param columnIndex Description of the Parameter
368: * @return The columnName value
369: */
370: public String getColumnName(int columnIndex) {
371: switch (columnIndex) {
372: case 0:
373: return res.getString("profileTypeTableHeading");
374: case 1:
375: return res.getString("profileNameTableHeading");
376: case 2:
377: return res.getString("profileUserTableHeading");
378: case 3:
379: return res.getString("profileHostTableHeading");
380: default:
381: return res.getString("profileRepositoryTableHeading");
382: }
383: }
384:
385: /**
386: * Gets the columnClass attribute of the CVSFileNodeTableModel object
387: *
388: * @param columnIndex Description of the Parameter
389: * @return The columnClass value
390: */
391: public Class getColumnClass(int columnIndex) {
392: return String.class;
393: }
394:
395: /**
396: * Gets the valueAt attribute of the CVSFileNodeTableModel object
397: *
398: * @param rowIndex Description of the Parameter
399: * @param columnIndex Description of the Parameter
400: * @return The valueAt value
401: */
402: public Object getValueAt(int rowIndex, int columnIndex) {
403: ConnectionProfile f = getConnectionProfileAt(rowIndex);
404: return getValueAt(f, columnIndex);
405: }
406:
407: private Object getValueAt(ConnectionProfile profile, int columnIndex) {
408: CVSRoot r = profile.getCVSRoot();
409: switch (columnIndex) {
410: case 0:
411: return (r == null) ? null : r.getConnectionType();
412: case 1:
413: return profile.getName();
414: case 2:
415: return (r == null) ? null : r.getUser();
416: case 3:
417: return (r == null) ? null : r.getHost();
418: default:
419: return (r == null) ? null : r.getRepository();
420: }
421: }
422:
423: /**
424: * Return the default profile
425: *
426: * @return default profile
427: */
428: public ConnectionProfile getDefaultProfile() {
429: return (defaultProfile != null) ? defaultProfile
430: : ((getRowCount() == 0) ? null
431: : getConnectionProfileAt(0));
432: }
433:
434: /**
435: * Return the default profile
436: *
437: * @return default profile
438: */
439: public void setDefaultProfile(ConnectionProfile defaultProfile) {
440: this .defaultProfile = defaultProfile;
441: fireTableDataChanged();
442: }
443:
444: /**
445: * Remove a connection profile at a specified index
446: *
447: * @param columnIndex Description of the Parameter
448: * @return The columnClass value
449: */
450: public void removeProfileAt(int r) {
451: profiles.removeElementAt(r);
452: resort();
453: }
454:
455: /**
456: * Add a connection profile
457: *
458: * @param columnIndex Description of the Parameter
459: * @return The columnClass value
460: */
461: public void addProfile(ConnectionProfile profile) {
462: addProfile(profile, true);
463: }
464:
465: private void addProfile(ConnectionProfile profile, boolean resort) {
466: int i = getRowCount();
467: profiles.addElement(profile);
468: if (resort) {
469: resort();
470: }
471: }
472:
473: class ConnectionProfileComparator implements Comparator {
474: public boolean equals(Object other) {
475: return (this .equals(other));
476: }
477:
478: public int compare(Object o1, Object o2) {
479: Object val1 = getValueAt((ConnectionProfile) o1,
480: sortCriteria.getSortType());
481: Object val2 = getValueAt((ConnectionProfile) o2,
482: sortCriteria.getSortType());
483: int c = val1.toString().compareTo(val2.toString());
484: return (sortCriteria.getSortDirection() == SortCriteria.SORT_ASCENDING) ? (c * -1)
485: : c;
486: }
487: }
488: }
|