KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > ui > search > SvnSearchView


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.subversion.ui.search;
20
21 import javax.swing.event.ListSelectionListener JavaDoc;
22 import org.openide.ErrorManager;
23 import org.netbeans.api.editor.mimelookup.MimeLookup;
24 import org.netbeans.api.editor.settings.FontColorSettings;
25
26 import javax.swing.*;
27 import javax.swing.text.*;
28 import java.awt.event.*;
29 import java.awt.*;
30 import java.awt.geom.Rectangle2D JavaDoc;
31 import java.text.DateFormat JavaDoc;
32 import org.tigris.subversion.svnclientadapter.ISVNLogMessage;
33 import org.tigris.subversion.svnclientadapter.SVNRevision;
34
35 /**
36  * Shows Search results in a JList.
37  *
38  * @author Tomas Stupka
39  */

40 class SvnSearchView implements ComponentListener {
41
42     private JList resultsList;
43     private ISVNLogMessage[] lm;
44     private AttributeSet searchHiliteAttrs;
45     private JScrollPane pane;
46                             
47                             
48     public SvnSearchView() {
49         FontColorSettings fcs = (FontColorSettings) MimeLookup.getMimeLookup("text/x-java").lookup(FontColorSettings.class); // NOI18N
50
searchHiliteAttrs = fcs.getFontColors("highlight-search"); // NOI18N
51

52         resultsList = new JList(new SvnSearchListModel());
53         resultsList.setFixedCellHeight(-1);
54         resultsList.setCellRenderer(new SvnSearchListCellRenderer());
55       // master.getAccessibleContext().setAccessibleName(NbBundle.getMessage(SvnSearchView.class, "ACSN_SummaryView_List"));
56
// master.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(SvnSearchView.class, "ACSD_SummaryView_List"));
57
resultsList.addComponentListener(this);
58         pane = new JScrollPane(resultsList, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
59     }
60
61     JComponent getComponent() {
62         return pane;
63     }
64     
65     public void componentResized(ComponentEvent e) {
66         int [] selection = resultsList.getSelectedIndices();
67         resultsList.setModel(new SvnSearchListModel());
68         resultsList.setSelectedIndices(selection);
69     }
70
71     public void componentHidden(ComponentEvent e) {
72         // not interested
73
}
74
75     public void componentMoved(ComponentEvent e) {
76         // not interested
77
}
78
79     public void componentShown(ComponentEvent e) {
80         // not interested
81
}
82     
83     public void setResults(ISVNLogMessage[] lm) {
84         this.lm = lm;
85         resultsList.setModel(new SvnSearchListModel());
86     }
87
88     SVNRevision getSelectedValue() {
89         Object JavaDoc selection = resultsList.getSelectedValue();
90         if(selection == null) {
91             return null;
92         }
93         if(!(selection instanceof ISVNLogMessage)) {
94             return null;
95         }
96         ISVNLogMessage message = (ISVNLogMessage) selection;
97         return message.getRevision();
98     }
99
100     void addListSelectionListener(ListSelectionListener JavaDoc listener) {
101         resultsList.addListSelectionListener(listener);
102     }
103
104     void removeListSelectionListener(ListSelectionListener JavaDoc listener) {
105         resultsList.removeListSelectionListener(listener);
106     }
107
108     private class SvnSearchListModel extends AbstractListModel {
109
110         public int getSize() {
111             if(lm == null) {
112                 return 0;
113             }
114             return lm.length;
115         }
116
117         public Object JavaDoc getElementAt(int index) {
118             return lm[index];
119         }
120     }
121     
122     private class SvnSearchListCellRenderer extends JPanel implements ListCellRenderer {
123
124         private static final String JavaDoc FIELDS_SEPARATOR = " "; // NOI18N
125
private static final double DARKEN_FACTOR = 0.95;
126
127         private Style selectedStyle;
128         private Style normalStyle;
129         private Style boldStyle;
130         private Style hiliteStyle;
131         
132         private JTextPane textPane = new JTextPane();
133         
134         private DateFormat JavaDoc defaultFormat;
135         
136         private int index;
137
138         public SvnSearchListCellRenderer() {
139             selectedStyle = textPane.addStyle("selected", null); // NOI18N
140
StyleConstants.setForeground(selectedStyle, UIManager.getColor("List.selectionForeground")); // NOI18N
141
normalStyle = textPane.addStyle("normal", null); // NOI18N
142
StyleConstants.setForeground(normalStyle, UIManager.getColor("List.foreground")); // NOI18N
143
boldStyle = textPane.addStyle("filename", normalStyle); // NOI18N
144
StyleConstants.setBold(boldStyle, true);
145             defaultFormat = DateFormat.getDateTimeInstance();
146
147             hiliteStyle = textPane.addStyle("hilite", normalStyle); // NOI18N
148
StyleConstants.setBackground(hiliteStyle, (Color) searchHiliteAttrs.getAttribute(StyleConstants.Background));
149             StyleConstants.setForeground(hiliteStyle, (Color) searchHiliteAttrs.getAttribute(StyleConstants.Foreground));
150             
151             setLayout(new BorderLayout());
152             add(textPane);
153             textPane.setBorder(null);
154         }
155         
156         public Color darker(Color c) {
157             return new Color(Math.max((int)(c.getRed() * DARKEN_FACTOR), 0),
158                  Math.max((int)(c.getGreen() * DARKEN_FACTOR), 0),
159                  Math.max((int)(c.getBlue() * DARKEN_FACTOR), 0));
160         }
161         
162         public Component getListCellRendererComponent(JList list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
163             if(value instanceof ISVNLogMessage) {
164                 ISVNLogMessage message = (ISVNLogMessage) value;
165                 StyledDocument sd = textPane.getStyledDocument();
166
167                 Style style;
168                 if (isSelected) {
169                     textPane.setBackground(UIManager.getColor("List.selectionBackground")); // NOI18N
170
style = selectedStyle;
171                 } else {
172                     Color c = UIManager.getColor("List.background"); // NOI18N
173
textPane.setBackground((index & 1) == 0 ? c : darker(c));
174                     style = normalStyle;
175                 }
176
177                 try {
178                     sd.remove(0, sd.getLength());
179                     sd.setCharacterAttributes(0, Integer.MAX_VALUE, style, true);
180                     sd.insertString(0, message.getRevision().toString(), null);
181                     sd.setCharacterAttributes(0, sd.getLength(), boldStyle, false);
182                     sd.insertString(sd.getLength(), FIELDS_SEPARATOR + message.getAuthor(), null);
183                     sd.insertString(sd.getLength(), FIELDS_SEPARATOR + defaultFormat.format(message.getDate()), null);
184                     sd.insertString(sd.getLength(), "\n" + message.getMessage(), null); // NOI18N
185
} catch (BadLocationException e) {
186                     ErrorManager.getDefault().notify(e);
187                 }
188                 
189                 if (message.getMessage() != null) {
190                     int width = resultsList.getWidth();
191                     if (width > 0) {
192                         FontMetrics fm = list.getFontMetrics(list.getFont());
193                         Rectangle2D JavaDoc rect = fm.getStringBounds(message.getMessage(), textPane.getGraphics());
194                         int nlc, i;
195                         for (nlc = -1, i = 0; i != -1 ; i = message.getMessage().indexOf('\n', i + 1), nlc++);
196                         //if (indentation == 0) nlc++;
197
int lines = (int) (rect.getWidth() / (width - 80) + 1);
198                         int ph = fm.getHeight() * (lines + nlc + 1) + 0;
199                         textPane.setPreferredSize(new Dimension(width - 50, ph));
200                     }
201                 }
202                 
203             }
204             return this;
205         }
206     }
207 }
208
Popular Tags