001: /*
002: * Copyright (C) 2001, 2002 Robert MacGrogan
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: *
019: * $Archive: SourceJammer$
020: * $FileName: GuiUtil.java$
021: * $FileID: 3991$
022: *
023: * Last change:
024: * $AuthorName: Rob MacGrogan$
025: * $Date: 9/1/03 4:47 PM$
026: * $Comment: Migrated show input box to MessageBoxUtil. Added
027: * ability to set type of each field in input box.$
028: */
029:
030: package org.sourcejammer.client.gui;
031:
032: import org.sourcejammer.client.gui.icons.IconBank;
033: import org.sourcejammer.client.gui.jsorttable.DefaultSortTableModel;
034: import org.sourcejammer.client.DisplayTextLibrary;
035: import java.awt.*;
036: import java.awt.event.ActionListener;
037: import java.awt.event.ActionEvent;
038: import javax.swing.*;
039: import javax.swing.table.*;
040: import javax.swing.tree.*;
041: import java.util.Vector;
042: import org.sourcejammer.project.*;
043: import org.sourcejammer.project.view.*;
044: import org.sourcejammer.util.AppConfig;
045: import org.sourcejammer.util.ConfigurationException;
046: import java.io.File;
047: import java.io.IOException;
048:
049: import org.sourcejammer.util.NameValue;
050: import org.sourcejammer.util.StringUtil;
051: import org.sourcejammer.util.BadMethodArgumentException;
052: import org.sourcejammer.util.SourceJammerConnectionException;
053: import org.sourcejammer.client.gui.conf.FileDialogWrapper;
054: import org.sourcejammer.client.gui.conf.FileNodeInfo;
055: import org.sourcejammer.client.gui.dialog.SJDialog;
056: import org.sourcejammer.client.SourceJammerClient;
057:
058: /**
059: * Title: $FileName: GuiUtil.java$
060: * @version $VerNum: 12$
061: * @author $AuthorName: Rob MacGrogan$<br><br>
062: *
063: * $Description: $<br>
064: * $KeyWordsOff: $
065: */
066:
067: public class GuiUtil {
068:
069: public static final String SKIN_LF_THEMES_DIR = "skins";
070:
071: public static final NameValue nv_NO_SPECIAL_ACTION = new NameValue(
072: DisplayTextLibrary
073: .displayText(DisplayTextLibrary.OPT_NO_ACTION),
074: SourceJammerClient.lfa_NO_SPECIAL_ACTION);
075:
076: public static final NameValue nv_SET_READ_ONLY = new NameValue(
077: DisplayTextLibrary
078: .displayText(DisplayTextLibrary.OPT_SET_LOCAL_COPY_READ_ONLY),
079: SourceJammerClient.lfa_SET_READ_ONLY);
080:
081: public static final NameValue nv_REMOVE_LOCAL = new NameValue(
082: DisplayTextLibrary
083: .displayText(DisplayTextLibrary.OPT_LOCAL_COPY_REMOVE),
084: SourceJammerClient.lfa_DELETE);
085:
086: public static final NameValue nv_GET_LATEST = new NameValue(
087: DisplayTextLibrary
088: .displayText(DisplayTextLibrary.OPT_DOWNLOAD_LATEST),
089: SourceJammerClient.lfa_GET_LATEST);
090:
091: private static FileDialogWrapper fileDialog = null;
092: private static final String PASSWORD_LABEL = DisplayTextLibrary
093: .displayText(DisplayTextLibrary.LBL_PASSWORD);
094:
095: static {
096: String fileDialogType = SourceJammerClient.getInstance()
097: .getFileDialogType();
098: fileDialog = new FileDialogWrapper();
099: if (fileDialogType.equals(SourceJammerClient.TREE_FILE_DIALOG)) {
100: fileDialog
101: .setDefaultFileDialog(FileDialogWrapper.TREE_FILE_DIALOG);
102: } else {
103: fileDialog
104: .setDefaultFileDialog(FileDialogWrapper.J_FILE_CHOOSER);
105: }
106: }
107:
108: private GuiUtil() {
109: }
110:
111: /**
112: * Display file dialog type specified in config file to select a file/directory.
113: */
114: public static File selectFile(boolean bShowFiles, String title,
115: String affirmText, File defaultDir, Frame parentFrame) {
116: return fileDialog.selectFile(bShowFiles, title, affirmText,
117: defaultDir, parentFrame);
118: }
119:
120: /**
121: * Display file dialog type specified in config file to select a file/directory.
122: */
123: public static File selectFile(boolean bShowFiles, String title,
124: String affirmText, File defaultDir, Dialog parentDialog) {
125:
126: return fileDialog.selectFile(bShowFiles, title, affirmText,
127: defaultDir, parentDialog);
128: }
129:
130: public static void setTablePreferredSizes() {
131: JTable table = CommandCentral.getInstance().getPrimaryTable();
132: //TableColumn colName = table.getColumn(Util.Columns.NAME);
133: //colName.setPreferredWidth(150);
134: //TableColumn colDate = table.getColumn(Util.Columns.CREATED_DATE);
135: //colDate.setPreferredWidth(100);
136: //table.sizeColumnsToFit(0);
137: //table.doLayout();
138: }
139:
140: public static final class Columns {
141: public static final String ICON = " ";
142: public static final String NAME = DisplayTextLibrary
143: .displayText(DisplayTextLibrary.COL_NAME);
144: public static final String DATE = DisplayTextLibrary
145: .displayText(DisplayTextLibrary.COL_DATE);
146: public static final String NUMBER_OF_VERSIONS = DisplayTextLibrary
147: .displayText(DisplayTextLibrary.COL_VER);
148: public static final String CHECK_OUT_USER = DisplayTextLibrary
149: .displayText(DisplayTextLibrary.COL_CK_OUT_TO);
150: public static final String CHECK_OUT_DATE = DisplayTextLibrary
151: .displayText(DisplayTextLibrary.COL_CK_OUT_DATE);
152: public static final String CHECK_OUT_PATH = DisplayTextLibrary
153: .displayText(DisplayTextLibrary.COL_PATH);
154: public static final String LOCAL_REMOTE_SYNC = "";
155: public static final String FILE_SIZE = DisplayTextLibrary
156: .displayText(DisplayTextLibrary.LBL_FILE_SIZE);
157: public static final int ICON_WIDTH = 20;
158: public static final int NAME_WIDTH = 170;
159: public static final int CREATE_DATE_WIDTH = 70;
160: public static final int NUM_VERSIONS_WIDTH = 15;
161: public static final int REMAINING_WIDTH = 30;
162: }
163:
164: // CustomTableModel allows a read-only table
165: public static class CustomTableModel extends DefaultSortTableModel {
166:
167: public boolean isCellEditable(int row, int column) {
168: return false; // all are not editable
169: }
170:
171: }
172:
173: public static DefaultSortTableModel getFileTableModel(
174: NodeIterator files) {
175: CustomTableModel model = new CustomTableModel();
176: // model.addColumn(Columns.ICON);
177: model.addColumn(Columns.NAME);
178: model.addColumn(Columns.LOCAL_REMOTE_SYNC);
179: model.addColumn(Columns.DATE);
180: model.addColumn(Columns.NUMBER_OF_VERSIONS);
181: model.addColumn(Columns.FILE_SIZE);
182: model.addColumn(Columns.CHECK_OUT_USER);
183: model.addColumn(Columns.CHECK_OUT_DATE);
184: //model.addColumn(Columns.CHECK_OUT_PATH);
185:
186: while (files.hasMoreNodes()) {
187: FileNodeInfo nd = (FileNodeInfo) files.getNextNode();
188: Vector vec = new Vector();
189: // vec.add(getFileIcon(nd));
190: vec.add(nd);
191: vec.add(nd.getLocalRemoteString());
192: vec.add(nd.getModifiedDate());
193: vec.add(Integer.toString(nd.getNumVersions()));
194: vec.add(new FileSize(nd.getSourceSizeInBytes()));
195: if (nd.isCheckedOut()) {
196: vec.add(nd.getCheckedOutToUser());
197: vec.add(nd.getCheckedOutDate());
198: //vec.add(nd.getCheckedOutToPath());
199: } else {
200: vec.add("");
201: vec.add("");
202: //vec.add("");
203: }
204: model.addRow(vec);
205: }
206: return model;
207: }
208:
209: private static final int BINARY = 1;
210: private static final int TEXT = 3;
211: private static final int LABEL = 5;
212: private static final int CHECKED_OUT = 9;
213: private static final int SHARED = 17;
214:
215: public static java.awt.Image getFileIcon(NodeInfo nd) {
216: java.awt.Image img = null;
217:
218: //Crazy bitwise stuff.
219: int testVal = 0;
220: if (nd.getFileType() == AppConfig.FileTypes.BINARY)
221: testVal += BINARY;
222: else if (nd.getFileType() == AppConfig.FileTypes.TEXT)
223: testVal += TEXT;
224: else
225: testVal += LABEL;
226: if (nd.isCheckedOut())
227: testVal += CHECKED_OUT;
228: if (nd.isShared())
229: testVal += SHARED;
230:
231: IconBank icons = IconBank.getInstance();
232:
233: switch (testVal) {
234: case BINARY:
235: img = icons.getImage(IconBank.BINARY);
236: break;
237: case TEXT:
238: img = icons.getImage(IconBank.TEXT);
239: break;
240: case LABEL:
241: img = icons.getImage(IconBank.LABEL);
242: break;
243: case BINARY + CHECKED_OUT:
244: img = icons.getImage(IconBank.BINARY_CHECKED);
245: break;
246: case TEXT + CHECKED_OUT:
247: img = icons.getImage(IconBank.TEXT_CHECKED);
248: break;
249: case LABEL + CHECKED_OUT:
250: img = icons.getImage(IconBank.LABEL_CHECKED);
251: break;
252: case BINARY + SHARED:
253: img = icons.getImage(IconBank.BINARY_SHARED);
254: break;
255: case TEXT + SHARED:
256: img = icons.getImage(IconBank.TEXT_SHARED);
257: break;
258: case LABEL + SHARED:
259: img = icons.getImage(IconBank.LABEL_SHARED);
260: break;
261: case BINARY + SHARED + CHECKED_OUT:
262: img = icons.getImage(IconBank.BINARY_SHARED_CHECKED);
263: break;
264: case TEXT + SHARED + CHECKED_OUT:
265: img = icons.getImage(IconBank.TEXT_SHARED_CHECKED);
266: break;
267: case LABEL + SHARED + CHECKED_OUT:
268: img = icons.getImage(IconBank.LABEL_SHARED_CHECKED);
269: break;
270: }
271: return img;
272: }
273:
274: public static void refreshTreeNodeContents(ProjectTreeNode node)
275: throws GUICommandException, SourceJammerConnectionException {
276: long lProjectID = node.getUniqueID();
277: Project ndThisProject = CommandCentral.getInstance()
278: .retrieveProject(lProjectID);
279:
280: UpdateStatus us = new UpdateStatus();
281: us.start();
282:
283: try {
284: node.setContents(ndThisProject);
285: } catch (NodeExistsException ex) {
286: throw new ConfigurationException(ex.getMessage(), ex);
287: }
288: }
289:
290: public static void setNewTableModel(TableModel model) {
291: CommandCentral oCentral = CommandCentral.getInstance();
292: JTable table = oCentral.getPrimaryTable();
293:
294: FileTable sTable = (FileTable) table;
295: DefaultSortTableModel sModel = (DefaultSortTableModel) model;
296: int iSortColumn = sTable.getSortedColumnIndex();
297: if (iSortColumn >= 0) {
298: sModel.sortColumn(iSortColumn, sTable
299: .isSortedColumnAscending());
300: }
301: table.setModel(model);
302: }
303:
304: public static VersionListTableModel getBaseVersionListTableModel() {
305: VersionListTableModel verListModel = new VersionListTableModel();
306: verListModel.addColumn(DisplayTextLibrary
307: .displayText(DisplayTextLibrary.COL_VER_NUMBER));
308: verListModel.addColumn(DisplayTextLibrary
309: .displayText(DisplayTextLibrary.LBL_ID));
310: verListModel.addColumn(DisplayTextLibrary
311: .displayText(DisplayTextLibrary.COL_DATE));
312: verListModel.addColumn(DisplayTextLibrary
313: .displayText(DisplayTextLibrary.COL_USER));
314: return verListModel;
315: }
316:
317: public static void setTableColumnToolTip(JTable table,
318: TableColumn col, String colName, String toolTip) {
319: TableCellRenderer headerRenderer = col.getHeaderRenderer();
320: if (headerRenderer == null) {
321: headerRenderer = new DefaultTableCellRenderer();
322: }
323: Component hdrComponent = headerRenderer
324: .getTableCellRendererComponent(table, colName, false,
325: false, 0, 0);
326: if (hdrComponent instanceof JComponent) {
327: JComponent c = (JComponent) hdrComponent;
328: c.setToolTipText(toolTip);
329: }
330: }
331:
332: public static DefaultTreeModel createTreeModel(Project root) {
333: NodeInfo ndRootInfo = new NodeInfo();
334: ndRootInfo.setUniqueID(root.getUniqueID());
335: ndRootInfo.setNodeType(AppConfig.NodeTypes.PROJECT);
336: ndRootInfo.setChildCount(root.childCount());
337: ndRootInfo.setCreatedDate(root.getCreatedDate());
338: boolean bHasChildren = false;
339: if (root.childCount() > 0) {
340: bHasChildren = true;
341: }
342: ndRootInfo.setHasChildren(bHasChildren);
343: ndRootInfo.setNodeName(root.getNodeName());
344: ndRootInfo.setShared(root.isShared());
345: ProjectTreeNode oTreeRoot = new ProjectTreeNode(ndRootInfo);
346: NodeName rootNodeName = new NodeName();
347: rootNodeName.setName(root.getNodeName());
348: oTreeRoot.setNodeName(rootNodeName);
349: try {
350: oTreeRoot.setContents(root);
351: } catch (org.sourcejammer.project.NodeExistsException ex) {
352: throw new ConfigurationException(
353: "Exception in createTreeModel.", ex);
354: }
355: return new DefaultTreeModel(oTreeRoot);
356: }
357:
358: public static GridBagConstraints setConstraints(
359: GridBagConstraints constraints, int x, int y, int width,
360: int height, int padx, int pady, int fill, int anchor) {
361: constraints.gridx = x;
362: constraints.gridy = y;
363: constraints.gridwidth = width;
364: constraints.gridheight = height;
365: constraints.ipadx = padx;
366: constraints.ipady = pady;
367: constraints.fill = fill;
368: constraints.anchor = anchor;
369: return constraints;
370: }
371:
372: /**
373: * Return a new NodeIterator with some contents as iter passed in, but
374: * sorted by NodeName.
375: */
376: public static NodeIterator sortNodeIterator(NodeIterator iter) {
377: return StringUtil.sortNodeIterator(iter);
378: }
379:
380: /**
381: * Return a new NodeIterator with some contents as iter passed in, but
382: * sorted by NodeName.
383: */
384: public static NodeIterator sortNodeIterator(NodeIterator iter,
385: boolean invert) {
386: return StringUtil.sortNodeIterator(iter, invert);
387: }
388:
389: /**
390: * Determines if any of the selected files in project are text files.
391: *
392: * @param project -- the currently selected ProjectTreeNode.
393: * @param selectedFileNames -- a String array of the file names of all of the
394: * files selected in project.
395: *
396: * @return <code>true</code> if at least one of the selected files is a Text file.
397: */
398: public static boolean areTextFilesSelected(ProjectTreeNode project,
399: String[] selectedFileNames)
400: throws NodeDoesNotExistException {
401: boolean bReturn = false;
402: int iNumFiles = selectedFileNames.length;
403: for (int i = 0; i < iNumFiles; i++) {
404: NodeInfo fileInfo = project
405: .getNodeInfo(selectedFileNames[i]);
406: if (fileInfo.getFileType() == AppConfig.FileTypes.TEXT
407: || fileInfo.getFileType() == AppConfig.FileTypes.LABEL) {
408: bReturn = true;
409: break;
410: }
411: }
412: return bReturn;
413: }
414:
415: public static boolean createDirectoryIfRequested(File dir)
416: throws IOException {
417: boolean bDirectoryCreated = false;
418: CommandCentral oCommand = CommandCentral.getInstance();
419: if (dir.isDirectory()) {
420: throw new BadMethodArgumentException(
421: "The File object passed in is not a directory: "
422: + dir.getAbsolutePath());
423: }
424: if (dir.exists()) {
425: throw new BadMethodArgumentException(
426: "The specified directory, " + dir.getAbsolutePath()
427: + " already exists.");
428: }
429:
430: SJPrimaryWindow win = CommandCentral.getInstance()
431: .getRootAppFrame();
432: String[] messages = {
433: DisplayTextLibrary
434: .displayText(DisplayTextLibrary.MSG_GET_PROJ_NO_DIR_1)
435: + dir.getAbsolutePath()
436: + " "
437: + DisplayTextLibrary
438: .displayText(DisplayTextLibrary.MSG_GET_PROJ_NO_DIR_2),
439: " ",
440: DisplayTextLibrary
441: .displayText(DisplayTextLibrary.MSG_GET_PROJ_NO_DIR_3),
442: " " };
443: int iResponse = JOptionPane.showConfirmDialog(oCommand
444: .getRootAppFrame(), messages, DisplayTextLibrary
445: .displayText(DisplayTextLibrary.LBL_CREATE_DIR),
446: JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
447: if (iResponse == JOptionPane.YES_OPTION) {
448: boolean success = dir.mkdirs();
449: if (success) {
450: bDirectoryCreated = true;
451: } else {
452: throw new IOException(
453: DisplayTextLibrary
454: .displayText(DisplayTextLibrary.ERR_CANT_MAKE_DIR));
455: }
456: } else {
457: bDirectoryCreated = false;
458: }
459: return bDirectoryCreated;
460: }
461:
462: public static JPanel topDownLabelComponent(String label,
463: Component component) {
464: JPanel panel = new JPanel(new GridBagLayout());
465: GridBagConstraints gbc = new GridBagConstraints();
466: setConstraints(gbc, 0, 0, 1, 1, 3, 4, GridBagConstraints.NONE,
467: gbc.WEST);
468: panel.add(new JLabel(label), gbc);
469: setConstraints(gbc, 0, 1, 1, 1, 3, 4, GridBagConstraints.NONE,
470: gbc.WEST);
471: JPanel container = new JPanel();
472: container.add(component);
473: panel.add(container, gbc);
474: JPanel parent = new JPanel();
475: parent.add(panel);
476: return parent;
477: }
478: }
|