1 33 34 package edu.rice.cs.drjava.ui; 35 36 import javax.swing.*; 37 import javax.swing.border.LineBorder ; 38 import javax.swing.border.EmptyBorder ; 39 import java.awt.*; 40 import java.util.LinkedList ; 41 import edu.rice.cs.drjava.model.OpenDefinitionsDocument; 42 import edu.rice.cs.drjava.DrJava; 43 import edu.rice.cs.drjava.config.*; 44 import edu.rice.cs.drjava.CodeStatus; 45 import edu.rice.cs.util.swing.DisplayManager; 46 47 48 public class RecentDocFrame extends JWindow { 49 MainFrame _frame; 51 52 DisplayManager<OpenDefinitionsDocument> _displayManager = _frame.getOddDisplayManager30(); 54 55 JLabel _label; 57 JPanel _panel; 59 JTextPane _textpane; 61 JScrollPane _scroller; 63 int _current = 0; 65 66 int _padding = 4; 67 68 LinkedList <OpenDefinitionsDocument> _docs = new LinkedList <OpenDefinitionsDocument>(); 69 70 private OptionListener<Color> _colorListener = new OptionListener<Color>() { 71 public void optionChanged(OptionEvent<Color> oce) { updateFontColor(); } 72 }; 73 74 private OptionListener<Font> _fontListener = new OptionListener<Font>() { 75 public void optionChanged(OptionEvent<Font> oce) { updateFontColor(); } 76 }; 77 78 private OptionListener<Boolean > _antialiasListener = new OptionListener<Boolean >() { 79 public void optionChanged(OptionEvent<Boolean > oce) { updateFontColor(); } 80 }; 81 82 private OptionListener<Boolean > _showSourceListener = new OptionListener<Boolean >() { 83 public void optionChanged(OptionEvent<Boolean > oce) { _showSource = oce.value; } 84 }; 85 86 87 boolean _antiAliasText = false; 88 89 90 boolean _showSource; 91 92 public RecentDocFrame(MainFrame f) { 93 super(); 94 _frame = f; 95 _current = 0; 96 _label = new JLabel("...") { 97 protected void paintComponent(Graphics g) { 99 if (CodeStatus.DEVELOPMENT) { 100 if (_antiAliasText && g instanceof Graphics2D) { 101 Graphics2D g2d = (Graphics2D)g; 102 g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, 103 RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 104 } 105 } 106 super.paintComponent(g); 107 } 108 }; 109 _panel = new JPanel(); 110 _scroller = new JScrollPane(); 111 _textpane = new JTextPane() { 112 protected void paintComponent(Graphics g) { 114 if (CodeStatus.DEVELOPMENT) { 115 if (_antiAliasText && g instanceof Graphics2D) { 116 Graphics2D g2d = (Graphics2D)g; 117 g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 118 } 119 } 120 super.paintComponent(g); 121 } 122 }; 123 124 _textpane.setText("..."); 125 _scroller.getViewport().add(_textpane); 126 _scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER); 127 _scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 128 _scroller.setMaximumSize(new Dimension(300,200)); 129 130 _panel.setLayout(new BorderLayout()); 131 _panel.add(_label, BorderLayout.NORTH); 132 _panel.add(_scroller, BorderLayout.SOUTH); 133 134 getContentPane().add(_panel); 135 pack(); 136 updateFontColor(); 137 _showSource = DrJava.getConfig().getSetting(OptionConstants.SHOW_SOURCE_WHEN_SWITCHING); 138 DrJava.getConfig().addOptionListener(OptionConstants.DEFINITIONS_BACKGROUND_COLOR, _colorListener); 139 DrJava.getConfig().addOptionListener(OptionConstants.DEFINITIONS_NORMAL_COLOR, _colorListener); 140 DrJava.getConfig().addOptionListener(OptionConstants.FONT_MAIN, _fontListener); 141 DrJava.getConfig().addOptionListener(OptionConstants.TEXT_ANTIALIAS, _antialiasListener); 142 DrJava.getConfig().addOptionListener(OptionConstants.SHOW_SOURCE_WHEN_SWITCHING, _showSourceListener); 143 } 144 145 private void updateFontColor() { 146 Font mainFont = DrJava.getConfig().getSetting(OptionConstants.FONT_MAIN); 147 Color backColor = DrJava.getConfig().getSetting(OptionConstants.DEFINITIONS_BACKGROUND_COLOR); 148 Color fontColor = DrJava.getConfig().getSetting(OptionConstants.DEFINITIONS_NORMAL_COLOR); 149 150 Font titleFont = mainFont.deriveFont((float) (mainFont.getSize() + 3)); 151 if (CodeStatus.DEVELOPMENT) 152 _antiAliasText = DrJava.getConfig().getSetting(OptionConstants.TEXT_ANTIALIAS).booleanValue(); 153 154 _label.setForeground(fontColor); 155 _panel.setBackground(backColor); 156 _label.setFont(titleFont); 157 _textpane.setForeground(fontColor); 158 _textpane.setFont(mainFont);; 159 _textpane.setBackground(backColor); 160 _scroller.setBackground(backColor); 161 _scroller.setBorder(new EmptyBorder (0,0,0,0)); 162 _panel.setBorder(new LineBorder (fontColor, 1)); 163 } 164 168 public void pokeDocument(OpenDefinitionsDocument d) { 169 if (_docs.contains(d)) { 170 _current = _docs.indexOf(d); 171 reset(); 172 } 173 else _docs.addFirst(d); 174 } 175 176 177 public void closeDocument(OpenDefinitionsDocument d) { _docs.remove(d); } 178 179 private void show(int _current) { 180 OpenDefinitionsDocument doc = _docs.get(_current); 181 182 String text = getTextFor(doc); 183 184 _label.setText(_displayManager.getName(doc)); 185 _label.setIcon(_displayManager.getIcon(doc)); 186 187 if (text.length() > 0) { 188 _textpane.setText(text); 190 _scroller.setPreferredSize(_textpane.getPreferredScrollableViewportSize()); 191 if (_scroller.getPreferredSize().getHeight() > 200) 192 _scroller.setPreferredSize(new Dimension((int)_scroller.getPreferredSize().getWidth(), 200)); 193 194 _scroller.setVisible(_showSource); 195 } 196 else _scroller.setVisible(false); 197 198 Dimension d = _label.getMinimumSize(); 199 d.setSize(d.getWidth() + _padding*2, d.getHeight() + _padding*2); 200 _label.setPreferredSize(d); 201 _label.setHorizontalAlignment(SwingConstants.CENTER); 202 _label.setVerticalAlignment(SwingConstants.CENTER); 203 pack(); 204 centerH(); 205 } 206 207 208 public void next() { 209 if (_docs.size() > 0) { 210 _current++; 211 if (_current >= _docs.size()) _current = 0; 212 show(_current); 213 } 214 } 215 216 217 public void prev() { 218 if (_docs.size() > 0) { 219 _current--; 220 if (_current < 0) _current = _docs.size() - 1; 221 show(_current); 222 } 223 } 224 225 private String getTextFor(OpenDefinitionsDocument doc) { 226 DefinitionsPane pane = _frame.getDefPaneGivenODD(doc); 227 String endl = System.getProperty("line.separator"); 228 int loc = pane.getCaretPosition(); 229 int start = loc; 230 int end = loc; 231 String text; 232 text = doc.getText(); 233 234 235 for (int i = 0; i < 4; i++) { 236 if (start > 0) start = text.lastIndexOf(endl, start-endl.length()); 237 } 238 if (start == -1) start = 0; 239 240 if (doc.getLength() >= endl.length() && text.substring(start, start+endl.length()).equals(endl)) 244 start += endl.length(); 245 246 int index; 247 for (int i=0;i<4;i++) { 248 if (end < doc.getLength()) { 249 index = text.indexOf(endl, end + endl.length()); 250 if (index != -1) end = index; 251 } 252 } 253 if (end < start) end = start; 254 text = text.substring(start, end); 255 return text; 256 } 257 258 259 public void first() { 260 _current = 0; 261 next(); 262 } 263 264 public void refreshColor() { } 265 266 267 public void setVisible(boolean v) { 268 centerH(); 269 if (_docs.size() > 0) { 270 if (v) { 271 centerV(); 272 refreshColor(); 273 first(); 274 } 275 else reset(); 276 super.setVisible(v); 277 } 278 } 279 280 281 private void centerH() { MainFrame.setPopupLoc(this, _frame); } 282 283 284 private void centerV() { 285 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 286 Dimension frameSize = getSize(); 287 setLocation((int)getLocation().getX(), (screenSize.height - frameSize.height) / 2); 288 } 289 290 291 public void reset() { 292 if (_current < _docs.size()) _docs.addFirst(_docs.remove(_current)); 293 } 294 295 296 public OpenDefinitionsDocument getDocument() { 297 if (_docs.size() > 0) return _docs.getFirst(); 298 return null; 299 } 300 301 307 } 308 | Popular Tags |