001: /*
002: * Sidebar.java
003: *
004: * Copyright (C) 2000-2003 Peter Graves
005: * $Id: Sidebar.java,v 1.4 2003/08/07 17:34:07 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.awt.Color;
025: import javax.swing.BorderFactory;
026: import javax.swing.BoxLayout;
027: import javax.swing.JComponent;
028: import javax.swing.JScrollPane;
029: import javax.swing.SwingUtilities;
030:
031: public final class Sidebar extends JComponent implements Constants {
032: private final Frame frame;
033: private final SplitPane splitPane;
034: private final SidebarPanel topPanel;
035: private final SidebarPanel bottomPanel;
036: private final SidebarBufferTree bufferTree;
037:
038: private NavigationComponent bottomComponent;
039: private int updateFlag;
040:
041: public Sidebar(Frame frame) {
042: this .frame = frame;
043: setLayout(new BoxLayout(this , BoxLayout.Y_AXIS));
044: topPanel = new SidebarPanel(this );
045: bufferTree = new SidebarBufferTree(this );
046: JScrollPane bufferListScrollPane = new JScrollPane(bufferTree);
047: bufferListScrollPane.setAlignmentX(LEFT_ALIGNMENT);
048: bufferListScrollPane
049: .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
050: bufferListScrollPane.setBorder(BorderFactory
051: .createEmptyBorder());
052: topPanel.addScrollPane(bufferListScrollPane);
053: bottomPanel = new SidebarPanel(this );
054: splitPane = new SplitPane(SplitPane.VERTICAL_SPLIT, topPanel,
055: bottomPanel);
056: splitPane.setBorder(BorderFactory.createEmptyBorder());
057: splitPane.setDividerLocation(Editor.getSessionProperties()
058: .getSidebarDividerLocation(frame));
059: add(splitPane);
060: }
061:
062: public final Frame getFrame() {
063: return frame;
064: }
065:
066: public final Editor getEditor() {
067: return frame.getCurrentEditor();
068: }
069:
070: public final SidebarBufferTree getBufferList() {
071: return bufferTree;
072: }
073:
074: public final SidebarBufferTree getBufferTree() {
075: return bufferTree;
076: }
077:
078: public NavigationComponent getBottomComponent() {
079: return bottomComponent;
080: }
081:
082: public void activateBufferList() {
083: if (bufferTree != null)
084: frame.setFocus(bufferTree);
085: }
086:
087: public void activateNavigationComponent() {
088: if (bottomComponent != null)
089: frame.setFocus((JComponent) bottomComponent);
090: }
091:
092: public int getDividerLocation() {
093: if (splitPane == null)
094: return -1;
095:
096: return splitPane.getDividerLocation();
097: }
098:
099: public final void setBufferListLabelText(String s) {
100: topPanel.setLabelText(s);
101: }
102:
103: public static void repaintBufferListInAllFrames() {
104: for (int i = 0; i < Editor.getFrameCount(); i++) {
105: Frame frame = Editor.getFrame(i);
106: if (frame != null) {
107: Sidebar sidebar = frame.getSidebar();
108: if (sidebar != null)
109: sidebar.getBufferList().repaint();
110: }
111: }
112: }
113:
114: public void setBuffer() {
115: if (bufferTree != null) {
116: Buffer buffer = frame.getCurrentEditor().getBuffer();
117: if (buffer != bufferTree.getSelectedBuffer())
118: bufferTree.setSelectedBuffer(buffer);
119: }
120: }
121:
122: public void updatePosition() {
123: if (bottomComponent != null)
124: bottomComponent.updatePosition();
125: }
126:
127: public static final boolean isTaggable(Buffer buffer) {
128: Mode mode = buffer.getMode();
129: return mode != null && mode.isTaggable();
130: }
131:
132: public synchronized final void setUpdateFlag(int mask) {
133: updateFlag |= mask;
134: if (bufferTree != null)
135: bufferTree.setUpdateFlag(updateFlag
136: & SIDEBAR_BUFFER_LIST_ALL);
137: }
138:
139: public static void setUpdateFlagInAllFrames(int mask) {
140: for (int i = 0; i < Editor.getFrameCount(); i++) {
141: Frame frame = Editor.getFrame(i);
142: Sidebar sidebar = frame.getSidebar();
143: if (sidebar != null)
144: sidebar.setUpdateFlag(mask);
145: }
146: }
147:
148: private void setBottomComponent() {
149: if (!SwingUtilities.isEventDispatchThread())
150: Debug
151: .bug("Sidebar.setBottomComponent() called from background thread!");
152: if (bottomComponent != null) {
153: Editor editor = frame.getCurrentEditor();
154: Buffer buffer = editor.getBuffer();
155: if (frame.getEditorCount() == 2 && buffer.isTransient()) {
156: editor = frame.getOtherEditor();
157: buffer = editor.getBuffer();
158: }
159: if (isTaggable(buffer)
160: && bottomComponent instanceof SidebarTagList) {
161: SidebarTagList list = (SidebarTagList) bottomComponent;
162: list.setEditor(editor);
163: if (list.getBuffer() == buffer)
164: return;
165: } else {
166: View view = editor.getCurrentView();
167: if (view != null
168: && bottomComponent == view
169: .getSidebarComponent())
170: return;
171: }
172: // Reaching here, we need to remove the old bottom component.
173: bottomComponent = null;
174: bottomPanel.removeAll();
175: }
176: Debug.assertTrue(bottomComponent == null);
177: final Editor editor = getEditor();
178: bottomComponent = editor.getMode().getSidebarComponent(editor);
179: if (bottomComponent != null) {
180: JScrollPane scrollPane = new JScrollPane(
181: (JComponent) bottomComponent);
182: if (bottomComponent instanceof SidebarList)
183: scrollPane
184: .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
185: scrollPane.setAlignmentX(LEFT_ALIGNMENT);
186: scrollPane.setBorder(BorderFactory.createEmptyBorder());
187: bottomPanel.addScrollPane(scrollPane);
188: bottomPanel.validate();
189: } else
190: bottomPanel.repaint();
191: }
192:
193: public synchronized void refreshSidebar() {
194: if (updateFlag != 0) {
195: if ((updateFlag & SIDEBAR_BUFFER_LIST_ALL) != 0) {
196: SidebarBufferTree bufferTree = getBufferTree();
197: if (bufferTree != null
198: && bufferTree != frame.getFocusedComponent())
199: bufferTree.updateBufferList();
200: }
201: if (bottomComponent == null
202: || bottomComponent != frame.getFocusedComponent()) {
203: setBottomComponent();
204: if (bottomComponent != null) {
205: bottomPanel.setLabelText(bottomComponent
206: .getLabelText());
207: // This might start a thread.
208: bottomComponent.refresh();
209: if ((updateFlag & SIDEBAR_POSITION) != 0)
210: bottomComponent.updatePosition();
211: }
212: }
213: updateFlag = 0;
214: }
215: }
216:
217: public static void refreshSidebarInAllFrames() {
218: if (!SwingUtilities.isEventDispatchThread())
219: Debug
220: .bug("refreshSidebarInAllFrames() called from background thread!");
221: for (int i = 0; i < Editor.getFrameCount(); i++) {
222: Frame frame = Editor.getFrame(i);
223: Sidebar sidebar = frame.getSidebar();
224: if (sidebar != null)
225: sidebar.refreshSidebar();
226: }
227: }
228: }
|