KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > ui > RecentDocFrame


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.ui;
35
36 import javax.swing.*;
37 import javax.swing.border.LineBorder JavaDoc;
38 import javax.swing.border.EmptyBorder JavaDoc;
39 import java.awt.*;
40 import java.util.LinkedList JavaDoc;
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 /** This class extends a Swing view class. Hence it should only be accessed from the event-handling thread. */
48 public class RecentDocFrame extends JWindow {
49   // MainFrame
50
MainFrame _frame;
51   
52   // The manager that gives filenames and icons
53
DisplayManager<OpenDefinitionsDocument> _displayManager = _frame.getOddDisplayManager30();
54     
55   // the label that shows the icon and filename
56
JLabel _label;
57   // the panel that holds the label and textpane
58
JPanel _panel;
59   // the pane that holds the sample of source
60
JTextPane _textpane;
61   // the scroller that holds the text
62
JScrollPane _scroller;
63   // the currently selected document
64
int _current = 0;
65   
66   int _padding = 4;
67   
68   LinkedList JavaDoc<OpenDefinitionsDocument> _docs = new LinkedList JavaDoc<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 JavaDoc> _antialiasListener = new OptionListener<Boolean JavaDoc>() {
79     public void optionChanged(OptionEvent<Boolean JavaDoc> oce) { updateFontColor(); }
80   };
81   
82   private OptionListener<Boolean JavaDoc> _showSourceListener = new OptionListener<Boolean JavaDoc>() {
83     public void optionChanged(OptionEvent<Boolean JavaDoc> oce) { _showSource = oce.value; }
84   };
85   
86   /* if the pane should antialias itself */
87   boolean _antiAliasText = false;
88   
89   /* if we should show source code when switching */
90   boolean _showSource;
91   
92   public RecentDocFrame(MainFrame f) {
93     super();
94     _frame = f;
95     _current = 0;
96     _label = new JLabel("...") {
97       // Enable anti-aliased text by overriding paintComponent.
98
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       // Enable anti-aliased text by overriding paintComponent.
113
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     /* make it bigger */
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 JavaDoc(0,0,0,0));
162     _panel.setBorder(new LineBorder JavaDoc(fontColor, 1));
163   }
164   /** Moves the document d to the beginning of the list
165    * if it's already in the list, or it adds it to the
166    * beginning if its not already in the list
167    */

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   /** Removes the document from the list. */
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 JavaDoc text = getTextFor(doc);
183       
184       _label.setText(_displayManager.getName(doc));
185       _label.setIcon(_displayManager.getIcon(doc));
186       
187       if (text.length() > 0) {
188         // as wide as the text area wants, but only 200px high
189
_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   /** Sets the current document to be the next document in the list. */
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   /** Sets the current document to be the previous document in the list. */
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 JavaDoc getTextFor(OpenDefinitionsDocument doc) {
226     DefinitionsPane pane = _frame.getDefPaneGivenODD(doc);
227     String JavaDoc endl = System.getProperty("line.separator");
228     int loc = pane.getCaretPosition();
229     int start = loc;
230     int end = loc;
231     String JavaDoc text;
232     text = doc.getText();
233     
234     /* get the starting point of 2 lines up... */
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     // skip the end line, if we're at one
241
// if (doc.getLength() >= endl.length() && text.substring(start, start+endl.length()) == endl)
242
// start += endl.length();
243
if (doc.getLength() >= endl.length() && text.substring(start, start+endl.length()).equals(endl))
244       start += endl.length();
245     /* get the ending point 2 lines down */
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   /** Resets the frame to point to the first document in the list. */
259   public void first() {
260     _current = 0;
261     next();
262   }
263   
264   public void refreshColor() { }
265   
266   /** Sets this frame as visible only if _docs is non empty. Also resets the frame accordingly */
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   /** Centers the frame in the screen. */
281   private void centerH() { MainFrame.setPopupLoc(this, _frame); }
282   
283   /** Centers the frame in the screen. */
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   /** Moves the selected document to the front of the list. */
291   public void reset() {
292     if (_current < _docs.size()) _docs.addFirst(_docs.remove(_current));
293   }
294   
295   /** Returns null if the list is empty, or the currently prefered OpenDefinitionsDocument. */
296   public OpenDefinitionsDocument getDocument() {
297     if (_docs.size() > 0) return _docs.getFirst();
298     return null;
299   }
300
301 // private ImageIcon _getIconResource(String name) {
302
// URL url = RecentDocFrame.class.getResource("icons/" + name);
303
// if (url != null) return new ImageIcon(url);
304
// return null;
305
// }
306

307 }
308
Popular Tags