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
009: * modify it under the terms of the GNU Library General Public License
010: * as published by the Free Software Foundation; either version 2 of
011: * the License, or (at your option) any later version.
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Library General Public License for more details.
016: *
017: * You should have received a copy of the GNU Library General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
020: */
021:
022: package gruntspud;
023:
024: import gruntspud.file.DirectoryStatus;
025: import gruntspud.ui.UIUtil;
026: import gruntspud.ui.icons.OverlayIcon;
027:
028: import java.io.File;
029: import java.io.FileInputStream;
030: import java.io.IOException;
031: import java.lang.reflect.Method;
032:
033: import javax.swing.Icon;
034: import javax.swing.ImageIcon;
035: import javax.swing.SwingConstants;
036: import javax.swing.UIManager;
037: import javax.swing.filechooser.FileSystemView;
038:
039: import org.netbeans.lib.cvsclient.file.FileStatus;
040:
041: /**
042: * Description of the Class
043: *
044: *@author magicthize
045: *@created 26 May 2002
046: */
047: public class CVSFileTypeUtil {
048: //
049: private static Method fsIconMethod;
050:
051: //
052: private static Method fsTypeDescriptionMethod;
053:
054: /**
055: * Get the substiution type given a line endings type
056: *
057: * @param lineEndings
058: * @return subst. type
059: */
060: public static CVSSubstType getSubstTypeForLineEndings(
061: int lineEndings) {
062: switch (lineEndings) {
063: case CVSFileNode.BINARY:
064: return CVSSubstType.CVS_SUBST_TYPE_BINARY;
065: case CVSFileNode.UNIX_LINE_ENDINGS:
066: case CVSFileNode.WINDOWS_LINE_ENDINGS:
067: case CVSFileNode.UNKNOWN_LINE_ENDINGS:
068: return CVSSubstType.CVS_SUBST_TYPE_TEXT;
069: case CVSFileNode.DIRECTORY:
070: return CVSSubstType.CVS_SUBST_TYPE_DIRECTORY;
071: default:
072: return null;
073: }
074: }
075:
076: /**
077: * Description of the Method
078: *
079: *@param file Description of the Parameter
080: *@return Description of the Return Value
081: */
082: public static int getLineEndings(File file) {
083: if (file.isDirectory()) {
084: return CVSFileNode.DIRECTORY;
085: }
086:
087: FileInputStream in = null;
088:
089: int lineEndings = CVSFileNode.UNKNOWN_LINE_ENDINGS;
090:
091: try {
092: in = new FileInputStream(file);
093:
094: byte[] buf = new byte[1024];
095: int z = in.read(buf);
096: int text = 0;
097: int win = 0;
098: int unix = 0;
099: byte l = 0;
100:
101: for (int i = 0; i < z; i++) {
102: /** @todo is more required here ??????? */
103: if ((buf[i] == 9) || (buf[i] == 26) || (buf[i] == 10)
104: || (buf[i] == 13)
105: || (buf[i] >= 32 && buf[i] != 127)) {
106: if (l == 13 && buf[i] == 10) {
107: win++;
108: } else if (l != 13 && buf[i] == 10) {
109: unix++;
110: }
111: text++;
112: }
113: l = buf[i];
114: }
115:
116: if (text == z) {
117: lineEndings = win > unix ? CVSFileNode.WINDOWS_LINE_ENDINGS
118: : (unix > win ? CVSFileNode.UNIX_LINE_ENDINGS
119: : CVSFileNode.UNKNOWN_LINE_ENDINGS);
120: } else {
121: lineEndings = CVSFileNode.BINARY;
122: }
123: } catch (IOException ioe) {
124: Constants.IO_LOG.error(ioe);
125: } finally {
126: GruntspudUtil.closeStream(in);
127: }
128:
129: return lineEndings;
130: }
131:
132: /**
133: * DOCUMENT ME!
134: *
135: * @param status DOCUMENT ME!
136: *
137: * @return DOCUMENT ME!
138: */
139: public static Icon getIconForStatus(FileStatus status) {
140: return getIconForStatus(status, null);
141: }
142:
143: /**
144: * DOCUMENT ME!
145: *
146: * @param status DOCUMENT ME!
147: * @param base DOCUMENT ME!
148: *
149: * @return DOCUMENT ME!
150: */
151: public static Icon getIconForStatus(FileStatus status, Icon base) {
152: Icon icon = null;
153:
154: if (base == null) {
155: base = UIManager.getIcon("Tree.leafIcon");
156: }
157: icon = new OverlayIcon(getStatusOverlayIcon(status, base),
158: base, SwingConstants.CENTER);
159: return icon;
160: }
161:
162: public static Icon getStatusOverlayIcon(FileStatus status,
163: Icon defaultIcon) {
164: if (status == FileStatus.HAS_CONFLICTS) {
165: return UIUtil
166: .getCachedIcon(Constants.ICON_STATUS_CONFLICTS_OVERLAY);
167: } else if (status == FileStatus.NEEDS_CHECKOUT) {
168: return UIUtil
169: .getCachedIcon(Constants.ICON_STATUS_NEEDS_CHECKOUT_OVERLAY);
170: } else if (status == FileStatus.NEEDS_MERGE) {
171: return UIUtil
172: .getCachedIcon(Constants.ICON_STATUS_NEEDS_MERGE_OVERLAY);
173: } else if (status == FileStatus.NEEDS_PATCH) {
174: return UIUtil
175: .getCachedIcon(Constants.ICON_STATUS_NEEDS_PATCH_OVERLAY);
176: } else if (status == FileStatus.ADDED) {
177: return UIUtil
178: .getCachedIcon(Constants.ICON_STATUS_ADDED_OVERLAY);
179: } else if (status == FileStatus.REMOVED) {
180: return UIUtil
181: .getCachedIcon(Constants.ICON_STATUS_REMOVED_OVERLAY);
182: } else if (status == FileStatus.UP_TO_DATE) {
183: return UIUtil
184: .getCachedIcon(Constants.ICON_STATUS_IN_CVS_OVERLAY);
185: } else if (status == null) {
186: return UIUtil
187: .getCachedIcon(Constants.ICON_STATUS_ERASED_OVERLAY);
188: } else if (status == FileStatus.MODIFIED) {
189: return UIUtil
190: .getCachedIcon(Constants.ICON_STATUS_OUT_OF_DATE_OVERLAY);
191: } else if (status == FileStatus.UNKNOWN) {
192: return defaultIcon;
193: } else if (status == null) {
194: return UIUtil
195: .getCachedIcon(Constants.ICON_STATUS_ERASED_OVERLAY);
196: }
197: return null;
198:
199: }
200:
201: /**
202: * DOCUMENT ME!
203: *
204: * @param status DOCUMENT ME!
205: * @param base DOCUMENT ME!
206: *
207: * @return DOCUMENT ME!
208: */
209: public static Icon getIconForStatus(DirectoryStatus status) {
210: Icon base = UIUtil
211: .getCachedIcon(Constants.ICON_TOOL_SMALL_DEFAULT_FOLDER_CLOSED);
212: Icon icon = null;
213:
214: if (status == DirectoryStatus.PROJECT) {
215: icon = new OverlayIcon(
216: UIUtil
217: .getCachedIcon(Constants.ICON_STATUS_PROJECT_OVERLAY),
218: base, SwingConstants.CENTER);
219: } else if (status == DirectoryStatus.MODULE) {
220: icon = new OverlayIcon(
221: UIUtil
222: .getCachedIcon(Constants.ICON_STATUS_MODULE_OVERLAY),
223: base, SwingConstants.CENTER);
224: } else if (status == DirectoryStatus.ATTENTION) {
225: icon = new OverlayIcon(
226: UIUtil
227: .getCachedIcon(Constants.ICON_STATUS_NEEDS_ATTENTION),
228: base, SwingConstants.CENTER);
229: } else if (status == DirectoryStatus.UP_TO_DATE) {
230: icon = new OverlayIcon(
231: UIUtil
232: .getCachedIcon(Constants.ICON_STATUS_IN_CVS_OVERLAY),
233: base, SwingConstants.CENTER);
234: } else {
235: icon = base;
236: }
237: return icon;
238: }
239:
240: /**
241: * Only for 1.4
242: * @param expanded
243: * @return
244: */
245: public static Icon getFileSystemIcon(File file) {
246: if (Gruntspud.is14()) {
247: try {
248: Method m = FileSystemView.class.getMethod(
249: "getSystemIcon", new Class[] { File.class });
250:
251: if (m != null) {
252: return (Icon) m
253: .invoke(FileSystemView.getFileSystemView(),
254: new Object[] { file });
255: }
256: } catch (Exception ioe) {
257: ioe.printStackTrace();
258: }
259: }
260:
261: return UIManager.getIcon("Tree.leafIcon");
262: }
263:
264: /**
265: * Only for 1.4
266: * @param expanded
267: * @return
268: */
269: public static String getFileSystemTypeDescriptionName(File file) {
270: String s = null;
271:
272: if (file.isDirectory()) {
273: s = "File Folder";
274: } else if (!file.exists()) {
275: s = "Missing!";
276: } else {
277: if (Gruntspud.is14()) {
278: try {
279: Method m = FileSystemView.class.getMethod(
280: "getSystemTypeDescription",
281: new Class[] { File.class });
282:
283: if (m != null) {
284: s = (String) m.invoke(FileSystemView
285: .getFileSystemView(),
286: new Object[] { file });
287: }
288: } catch (Exception ioe) {
289: }
290: }
291: }
292:
293: if (s == null) {
294: int idx = file.getName().lastIndexOf('.');
295:
296: if (idx != -1) {
297: s = file.getName().substring(idx + 1).toUpperCase();
298: }
299: }
300:
301: return s;
302: }
303:
304: public static String createToolTipTextForNode(CVSFileNode node) {
305: StringBuffer buf = new StringBuffer();
306: buf.append("<html>");
307: if (node.isNeedsAttention()) {
308: buf.append("<h3><i>");
309: buf.append("<img src=\"");
310: buf
311: .append(((ImageIcon) UIUtil
312: .getCachedIcon(Constants.ICON_STATUS_NEEDS_ATTENTION))
313: .getDescription());
314: buf.append("\">");
315: buf.append("Needs attention");
316: buf.append("</i></h3>");
317:
318: } else if (node.isProjectRoot()) {
319: buf.append("<h3><i>");
320: buf.append("<img src=\"");
321: buf
322: .append(((ImageIcon) UIUtil
323: .getCachedIcon(Constants.ICON_STATUS_PROJECT_OVERLAY))
324: .getDescription());
325: buf.append("\">");
326: buf.append("Project");
327: buf.append("</i></h3>");
328: } else if (node.isModuleRoot()) {
329: buf.append("<h3><i>");
330: buf.append("<img src=\"");
331: buf
332: .append(((ImageIcon) UIUtil
333: .getCachedIcon(Constants.ICON_STATUS_MODULE_OVERLAY))
334: .getDescription());
335: buf.append("\">");
336: buf.append("Module Root");
337: buf.append("</i></h3>");
338: } else {
339: FileStatus status = node.getOverallStatus();
340: if (status != null) {
341: Icon icon = getStatusOverlayIcon(node
342: .getOverallStatus(), null);
343: buf.append("<h3><i>");
344: if (icon != null) {
345: buf.append("<img src=\"");
346: buf.append(((ImageIcon) icon).getDescription());
347: buf.append("\">");
348: }
349: buf.append(status.toString());
350: buf.append("</i></h3>");
351: } else {
352: buf.append("<h3><i><b>");
353: buf.append("<img src=\"");
354: buf
355: .append(((ImageIcon) UIUtil
356: .getCachedIcon(Constants.ICON_STATUS_ERASED_OVERLAY))
357: .getDescription());
358: buf.append("\">");
359: buf.append("<font color=\"red\">Missing!</font>");
360: buf.append("</b></i></h3>");
361:
362: }
363: }
364: buf.append("<p>");
365: buf.append(node.getFile().getAbsolutePath());
366: buf.append("</p>");
367: return buf.toString();
368: }
369: }
|