1 19 package org.netbeans.modules.subversion.ui.search; 20 21 import javax.swing.event.ListSelectionListener ; 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 ; 31 import java.text.DateFormat ; 32 import org.tigris.subversion.svnclientadapter.ISVNLogMessage; 33 import org.tigris.subversion.svnclientadapter.SVNRevision; 34 35 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); searchHiliteAttrs = fcs.getFontColors("highlight-search"); 52 resultsList = new JList(new SvnSearchListModel()); 53 resultsList.setFixedCellHeight(-1); 54 resultsList.setCellRenderer(new SvnSearchListCellRenderer()); 55 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 } 74 75 public void componentMoved(ComponentEvent e) { 76 } 78 79 public void componentShown(ComponentEvent e) { 80 } 82 83 public void setResults(ISVNLogMessage[] lm) { 84 this.lm = lm; 85 resultsList.setModel(new SvnSearchListModel()); 86 } 87 88 SVNRevision getSelectedValue() { 89 Object 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 listener) { 101 resultsList.addListSelectionListener(listener); 102 } 103 104 void removeListSelectionListener(ListSelectionListener 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 getElementAt(int index) { 118 return lm[index]; 119 } 120 } 121 122 private class SvnSearchListCellRenderer extends JPanel implements ListCellRenderer { 123 124 private static final String FIELDS_SEPARATOR = " "; 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 defaultFormat; 135 136 private int index; 137 138 public SvnSearchListCellRenderer() { 139 selectedStyle = textPane.addStyle("selected", null); StyleConstants.setForeground(selectedStyle, UIManager.getColor("List.selectionForeground")); normalStyle = textPane.addStyle("normal", null); StyleConstants.setForeground(normalStyle, UIManager.getColor("List.foreground")); boldStyle = textPane.addStyle("filename", normalStyle); StyleConstants.setBold(boldStyle, true); 145 defaultFormat = DateFormat.getDateTimeInstance(); 146 147 hiliteStyle = textPane.addStyle("hilite", normalStyle); 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 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")); style = selectedStyle; 171 } else { 172 Color c = UIManager.getColor("List.background"); 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); } 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 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 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 |