private String getTextFor(OpenDefinitionsDocument doc) { DefinitionsPane pane = _frame.getDefPaneGivenODD(doc); String endl = "\n"; // was StringOps.EOL; but Swing uses '\n' for newLine int loc = pane.getCaretPosition(); int start = loc; int end = loc; String text; text = doc.getText(); /* get the starting point of 2 lines up... */ for (int i = 0; i < 4; i++) { if (start > 0) start = text.lastIndexOf(endl, start - endl.length()); } if (start == -1) start = 0; // skip the end line, if we're at one // if (doc.getLength() >= endl.length() && text.substring(start, start+endl.length()) == // endl) // start += endl.length(); if (doc.getLength() >= endl.length() && text.substring(start, start + endl.length()).equals(endl)) start += endl.length(); /* get the ending point 2 lines down */ int index; for (int i = 0; i < 4; i++) { if (end < doc.getLength()) { index = text.indexOf(endl, end + endl.length()); if (index != -1) end = index; } } if (end < start) end = start; text = text.substring(start, end); return text; }
/** * This class extends a Swing view class. Hence it should only be accessed from the event-handling * thread. */ public class RecentDocFrame extends JWindow { // MainFrame MainFrame _frame; // The manager that gives filenames and icons DisplayManager<OpenDefinitionsDocument> _displayManager = MainFrame.getOddDisplayManager30(); // the label that shows the icon and filename JLabel _label; // the panel that holds the label and textpane JPanel _panel; // the pane that holds the sample of source JTextPane _textpane; // the scroller that holds the text JScrollPane _scroller; // the currently selected document int _current = 0; int _padding = 4; LinkedList<OpenDefinitionsDocument> _docs = new LinkedList<OpenDefinitionsDocument>(); private OptionListener<Color> _colorListener = new OptionListener<Color>() { public void optionChanged(OptionEvent<Color> oce) { updateFontColor(); } }; private OptionListener<Font> _fontListener = new OptionListener<Font>() { public void optionChanged(OptionEvent<Font> oce) { updateFontColor(); } }; private OptionListener<Boolean> _antialiasListener = new OptionListener<Boolean>() { public void optionChanged(OptionEvent<Boolean> oce) { updateFontColor(); } }; private OptionListener<Boolean> _showSourceListener = new OptionListener<Boolean>() { public void optionChanged(OptionEvent<Boolean> oce) { _showSource = oce.value; } }; /* if the pane should antialias itself */ boolean _antiAliasText = false; /* if we should show source code when switching */ boolean _showSource; public RecentDocFrame(MainFrame f) { super(); _frame = f; _current = 0; _label = new JLabel("...") { // Enable anti-aliased text by overriding paintComponent. protected void paintComponent(Graphics g) { if (_antiAliasText && g instanceof Graphics2D) { Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); } super.paintComponent(g); } }; _panel = new JPanel(); _scroller = new JScrollPane(); _textpane = new JTextPane() { // Enable anti-aliased text by overriding paintComponent. protected void paintComponent(Graphics g) { if (_antiAliasText && g instanceof Graphics2D) { Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); } super.paintComponent(g); } }; _textpane.setText("..."); _scroller.getViewport().add(_textpane); _scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER); _scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); _scroller.setMaximumSize(new Dimension(300, 200)); _panel.setLayout(new BorderLayout()); _panel.add(_label, BorderLayout.NORTH); _panel.add(_scroller, BorderLayout.SOUTH); getContentPane().add(_panel); pack(); updateFontColor(); _showSource = DrJava.getConfig().getSetting(OptionConstants.SHOW_SOURCE_WHEN_SWITCHING); DrJava.getConfig() .addOptionListener(OptionConstants.DEFINITIONS_BACKGROUND_COLOR, _colorListener); DrJava.getConfig().addOptionListener(OptionConstants.DEFINITIONS_NORMAL_COLOR, _colorListener); DrJava.getConfig().addOptionListener(OptionConstants.FONT_MAIN, _fontListener); DrJava.getConfig().addOptionListener(OptionConstants.TEXT_ANTIALIAS, _antialiasListener); DrJava.getConfig() .addOptionListener(OptionConstants.SHOW_SOURCE_WHEN_SWITCHING, _showSourceListener); } private void updateFontColor() { Font mainFont = DrJava.getConfig().getSetting(OptionConstants.FONT_MAIN); Color backColor = DrJava.getConfig().getSetting(OptionConstants.DEFINITIONS_BACKGROUND_COLOR); Color fontColor = DrJava.getConfig().getSetting(OptionConstants.DEFINITIONS_NORMAL_COLOR); /* make it bigger */ Font titleFont = mainFont.deriveFont((float) (mainFont.getSize() + 3)); _antiAliasText = DrJava.getConfig().getSetting(OptionConstants.TEXT_ANTIALIAS).booleanValue(); _label.setForeground(fontColor); _panel.setBackground(backColor); _label.setFont(titleFont); _textpane.setForeground(fontColor); _textpane.setFont(mainFont); ; _textpane.setBackground(backColor); _scroller.setBackground(backColor); _scroller.setBorder(new EmptyBorder(0, 0, 0, 0)); _panel.setBorder(new LineBorder(fontColor, 1)); } /** * Moves the document d to the beginning of the list if it's already in the list, or it adds it to * the beginning if its not already in the list. */ public void pokeDocument(OpenDefinitionsDocument d) { if (_docs.contains(d)) { _current = _docs.indexOf(d); reset(); } else _docs.addFirst(d); } /** Removes the document from the list. */ public void closeDocument(OpenDefinitionsDocument d) { _docs.remove(d); } private void show(int _current) { OpenDefinitionsDocument doc = _docs.get(_current); String text = getTextFor(doc); _label.setText(_displayManager.getName(doc)); _label.setIcon(_displayManager.getIcon(doc)); if (text.length() > 0) { // as wide as the text area wants, but only 200px high _textpane.setText(text); _scroller.setPreferredSize(_textpane.getPreferredScrollableViewportSize()); if (_scroller.getPreferredSize().getHeight() > 200) _scroller.setPreferredSize( new Dimension((int) _scroller.getPreferredSize().getWidth(), 200)); _scroller.setVisible(_showSource); } else _scroller.setVisible(false); Dimension d = _label.getMinimumSize(); d.setSize(d.getWidth() + _padding * 2, d.getHeight() + _padding * 2); _label.setPreferredSize(d); _label.setHorizontalAlignment(SwingConstants.CENTER); _label.setVerticalAlignment(SwingConstants.CENTER); pack(); centerH(); } /** Sets the current document to be the next document in the list. */ public void next() { if (_docs.size() > 0) { _current++; if (_current >= _docs.size()) _current = 0; show(_current); } } /** Sets the current document to be the previous document in the list. */ public void prev() { if (_docs.size() > 0) { _current--; if (_current < 0) _current = _docs.size() - 1; show(_current); } } private String getTextFor(OpenDefinitionsDocument doc) { DefinitionsPane pane = _frame.getDefPaneGivenODD(doc); String endl = "\n"; // was StringOps.EOL; but Swing uses '\n' for newLine int loc = pane.getCaretPosition(); int start = loc; int end = loc; String text; text = doc.getText(); /* get the starting point of 2 lines up... */ for (int i = 0; i < 4; i++) { if (start > 0) start = text.lastIndexOf(endl, start - endl.length()); } if (start == -1) start = 0; // skip the end line, if we're at one // if (doc.getLength() >= endl.length() && text.substring(start, start+endl.length()) == // endl) // start += endl.length(); if (doc.getLength() >= endl.length() && text.substring(start, start + endl.length()).equals(endl)) start += endl.length(); /* get the ending point 2 lines down */ int index; for (int i = 0; i < 4; i++) { if (end < doc.getLength()) { index = text.indexOf(endl, end + endl.length()); if (index != -1) end = index; } } if (end < start) end = start; text = text.substring(start, end); return text; } /** Resets the frame to point to the first document in the list. */ public void first() { _current = 0; next(); } public void refreshColor() {} /** Sets this frame as visible only if _docs is non empty. Also resets the frame accordingly */ public void setVisible(boolean v) { centerH(); if (_docs.size() > 0) { if (v) { centerV(); refreshColor(); first(); } else reset(); super.setVisible(v); } } /** Centers the frame in the screen. */ private void centerH() { Utilities.setPopupLoc(this, _frame); } /** Centers the frame in the screen. */ private void centerV() { Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = getSize(); setLocation((int) getLocation().getX(), (screenSize.height - frameSize.height) / 2); } /** Moves the selected document to the front of the list. */ public void reset() { if (_current < _docs.size()) _docs.addFirst(_docs.remove(_current)); } /** Returns null if the list is empty, or the currently prefered OpenDefinitionsDocument. */ public OpenDefinitionsDocument getDocument() { if (_docs.size() > 0) return _docs.getFirst(); return null; } // private ImageIcon _getIconResource(String name) { // URL url = RecentDocFrame.class.getResource("icons/" + name); // if (url != null) return new ImageIcon(url); // return null; // } }