001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.subversion.ui.search;
042:
043: import javax.swing.event.ListSelectionListener;
044: import org.netbeans.api.editor.mimelookup.MimeLookup;
045: import org.netbeans.api.editor.settings.FontColorSettings;
046:
047: import javax.swing.*;
048: import javax.swing.text.*;
049: import java.awt.event.*;
050: import java.awt.*;
051: import java.awt.geom.Rectangle2D;
052: import java.text.DateFormat;
053: import java.util.logging.Level;
054: import org.netbeans.modules.subversion.Subversion;
055: import org.openide.util.NbBundle;
056: import org.tigris.subversion.svnclientadapter.ISVNLogMessage;
057: import org.tigris.subversion.svnclientadapter.SVNRevision;
058:
059: /**
060: * Shows Search results in a JList.
061: *
062: * @author Tomas Stupka
063: */
064: class SvnSearchView implements ComponentListener {
065:
066: private JList resultsList;
067: private ISVNLogMessage[] lm;
068: private AttributeSet searchHiliteAttrs;
069: private JScrollPane pane;
070:
071: public SvnSearchView() {
072: FontColorSettings fcs = (FontColorSettings) MimeLookup
073: .getMimeLookup("text/x-java").lookup(
074: FontColorSettings.class); // NOI18N
075: searchHiliteAttrs = fcs.getFontColors("highlight-search"); // NOI18N
076:
077: resultsList = new JList(new SvnSearchListModel());
078: resultsList.setFixedCellHeight(-1);
079: resultsList.setCellRenderer(new SvnSearchListCellRenderer());
080: resultsList.getAccessibleContext().setAccessibleName(
081: NbBundle.getMessage(SvnSearchView.class,
082: "ACSN_SummaryView_ListName"));
083: resultsList.getAccessibleContext().setAccessibleDescription(
084: NbBundle.getMessage(SvnSearchView.class,
085: "ACSD_SummaryView_ListDesc"));
086: resultsList.addComponentListener(this );
087: pane = new JScrollPane(resultsList,
088: JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
089: JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
090: }
091:
092: JComponent getComponent() {
093: return pane;
094: }
095:
096: public void componentResized(ComponentEvent e) {
097: int[] selection = resultsList.getSelectedIndices();
098: resultsList.setModel(new SvnSearchListModel());
099: resultsList.setSelectedIndices(selection);
100: }
101:
102: public void componentHidden(ComponentEvent e) {
103: // not interested
104: }
105:
106: public void componentMoved(ComponentEvent e) {
107: // not interested
108: }
109:
110: public void componentShown(ComponentEvent e) {
111: // not interested
112: }
113:
114: public void setResults(ISVNLogMessage[] lm) {
115: this .lm = lm;
116: resultsList.setModel(new SvnSearchListModel());
117: }
118:
119: SVNRevision getSelectedValue() {
120: Object selection = resultsList.getSelectedValue();
121: if (selection == null) {
122: return null;
123: }
124: if (!(selection instanceof ISVNLogMessage)) {
125: return null;
126: }
127: ISVNLogMessage message = (ISVNLogMessage) selection;
128: return message.getRevision();
129: }
130:
131: void addListSelectionListener(ListSelectionListener listener) {
132: resultsList.addListSelectionListener(listener);
133: }
134:
135: void removeListSelectionListener(ListSelectionListener listener) {
136: resultsList.removeListSelectionListener(listener);
137: }
138:
139: private class SvnSearchListModel extends AbstractListModel {
140:
141: public int getSize() {
142: if (lm == null) {
143: return 0;
144: }
145: return lm.length;
146: }
147:
148: public Object getElementAt(int index) {
149: return lm[index];
150: }
151: }
152:
153: private class SvnSearchListCellRenderer extends JPanel implements
154: ListCellRenderer {
155:
156: private static final String FIELDS_SEPARATOR = " "; // NOI18N
157: private static final double DARKEN_FACTOR = 0.95;
158:
159: private Style selectedStyle;
160: private Style normalStyle;
161: private Style boldStyle;
162: private Style hiliteStyle;
163:
164: private JTextPane textPane = new JTextPane();
165:
166: private DateFormat defaultFormat;
167:
168: public SvnSearchListCellRenderer() {
169: selectedStyle = textPane.addStyle("selected", null); // NOI18N
170: StyleConstants.setForeground(selectedStyle, UIManager
171: .getColor("List.selectionForeground")); // NOI18N
172: normalStyle = textPane.addStyle("normal", null); // NOI18N
173: StyleConstants.setForeground(normalStyle, UIManager
174: .getColor("List.foreground")); // NOI18N
175: boldStyle = textPane.addStyle("filename", normalStyle); // NOI18N
176: StyleConstants.setBold(boldStyle, true);
177: defaultFormat = DateFormat.getDateTimeInstance();
178:
179: hiliteStyle = textPane.addStyle("hilite", normalStyle); // NOI18N
180: Color c = (Color) searchHiliteAttrs
181: .getAttribute(StyleConstants.Background);
182: if (c != null)
183: StyleConstants.setBackground(hiliteStyle, c);
184: c = (Color) searchHiliteAttrs
185: .getAttribute(StyleConstants.Foreground);
186: if (c != null)
187: StyleConstants.setForeground(hiliteStyle, c);
188:
189: setLayout(new BorderLayout());
190: add(textPane);
191: textPane.setBorder(null);
192: }
193:
194: public Color darker(Color c) {
195: return new Color(Math.max(
196: (int) (c.getRed() * DARKEN_FACTOR), 0), Math.max(
197: (int) (c.getGreen() * DARKEN_FACTOR), 0), Math.max(
198: (int) (c.getBlue() * DARKEN_FACTOR), 0));
199: }
200:
201: public Component getListCellRendererComponent(JList list,
202: Object value, int index, boolean isSelected,
203: boolean cellHasFocus) {
204: if (value instanceof ISVNLogMessage) {
205: ISVNLogMessage message = (ISVNLogMessage) value;
206: StyledDocument sd = textPane.getStyledDocument();
207:
208: Style style;
209: if (isSelected) {
210: textPane.setBackground(UIManager
211: .getColor("List.selectionBackground")); // NOI18N
212: style = selectedStyle;
213: } else {
214: Color c = UIManager.getColor("List.background"); // NOI18N
215: textPane.setBackground((index & 1) == 0 ? c
216: : darker(c));
217: style = normalStyle;
218: }
219:
220: try {
221: sd.remove(0, sd.getLength());
222: sd.setCharacterAttributes(0, Integer.MAX_VALUE,
223: style, true);
224: sd.insertString(0,
225: message.getRevision().toString(), null);
226: sd.setCharacterAttributes(0, sd.getLength(),
227: boldStyle, false);
228: sd.insertString(sd.getLength(), FIELDS_SEPARATOR
229: + message.getAuthor(), null);
230: sd.insertString(sd.getLength(), FIELDS_SEPARATOR
231: + defaultFormat.format(message.getDate()),
232: null);
233: sd.insertString(sd.getLength(), "\n"
234: + message.getMessage(), null); // NOI18N
235: } catch (BadLocationException e) {
236: Subversion.LOG.log(Level.SEVERE, null, e);
237: }
238:
239: if (message.getMessage() != null) {
240: int width = resultsList.getWidth();
241: if (width > 0) {
242: FontMetrics fm = list.getFontMetrics(list
243: .getFont());
244: Rectangle2D rect = fm.getStringBounds(message
245: .getMessage(), textPane.getGraphics());
246: int nlc, i;
247: for (nlc = -1, i = 0; i != -1; i = message
248: .getMessage().indexOf('\n', i + 1), nlc++)
249: ;
250: //if (indentation == 0) nlc++;
251: int lines = (int) (rect.getWidth()
252: / (width - 80) + 1);
253: int ph = fm.getHeight() * (lines + nlc + 1) + 0;
254: textPane.setPreferredSize(new Dimension(
255: width - 50, ph));
256: }
257: }
258:
259: }
260: return this;
261: }
262: }
263: }
|