1 21 22 package org.armedbear.j; 23 24 import java.awt.Color ; 25 import javax.swing.BorderFactory ; 26 import javax.swing.BoxLayout ; 27 import javax.swing.JComponent ; 28 import javax.swing.JScrollPane ; 29 import javax.swing.SwingUtilities ; 30 31 public final class Sidebar extends JComponent implements Constants 32 { 33 private final Frame frame; 34 private final SplitPane splitPane; 35 private final SidebarPanel topPanel; 36 private final SidebarPanel bottomPanel; 37 private final SidebarBufferTree bufferTree; 38 39 private NavigationComponent bottomComponent; 40 private int updateFlag; 41 42 public Sidebar(Frame frame) 43 { 44 this.frame = frame; 45 setLayout(new BoxLayout (this, BoxLayout.Y_AXIS)); 46 topPanel = new SidebarPanel(this); 47 bufferTree = new SidebarBufferTree(this); 48 JScrollPane bufferListScrollPane = new JScrollPane (bufferTree); 49 bufferListScrollPane.setAlignmentX(LEFT_ALIGNMENT); 50 bufferListScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 51 bufferListScrollPane.setBorder(BorderFactory.createEmptyBorder()); 52 topPanel.addScrollPane(bufferListScrollPane); 53 bottomPanel = new SidebarPanel(this); 54 splitPane = new SplitPane(SplitPane.VERTICAL_SPLIT, topPanel, bottomPanel); 55 splitPane.setBorder(BorderFactory.createEmptyBorder()); 56 splitPane.setDividerLocation(Editor.getSessionProperties().getSidebarDividerLocation(frame)); 57 add(splitPane); 58 } 59 60 public final Frame getFrame() 61 { 62 return frame; 63 } 64 65 public final Editor getEditor() 66 { 67 return frame.getCurrentEditor(); 68 } 69 70 public final SidebarBufferTree getBufferList() 71 { 72 return bufferTree; 73 } 74 75 public final SidebarBufferTree getBufferTree() 76 { 77 return bufferTree; 78 } 79 80 public NavigationComponent getBottomComponent() 81 { 82 return bottomComponent; 83 } 84 85 public void activateBufferList() 86 { 87 if (bufferTree != null) 88 frame.setFocus(bufferTree); 89 } 90 91 public void activateNavigationComponent() 92 { 93 if (bottomComponent != null) 94 frame.setFocus((JComponent ) bottomComponent); 95 } 96 97 public int getDividerLocation() 98 { 99 if (splitPane == null) 100 return -1; 101 102 return splitPane.getDividerLocation(); 103 } 104 105 public final void setBufferListLabelText(String s) 106 { 107 topPanel.setLabelText(s); 108 } 109 110 public static void repaintBufferListInAllFrames() 111 { 112 for (int i = 0; i < Editor.getFrameCount(); i++) { 113 Frame frame = Editor.getFrame(i); 114 if (frame != null) { 115 Sidebar sidebar = frame.getSidebar(); 116 if (sidebar != null) 117 sidebar.getBufferList().repaint(); 118 } 119 } 120 } 121 122 public void setBuffer() 123 { 124 if (bufferTree != null) { 125 Buffer buffer = frame.getCurrentEditor().getBuffer(); 126 if (buffer != bufferTree.getSelectedBuffer()) 127 bufferTree.setSelectedBuffer(buffer); 128 } 129 } 130 131 public void updatePosition() 132 { 133 if (bottomComponent != null) 134 bottomComponent.updatePosition(); 135 } 136 137 public static final boolean isTaggable(Buffer buffer) 138 { 139 Mode mode = buffer.getMode(); 140 return mode != null && mode.isTaggable(); 141 } 142 143 public synchronized final void setUpdateFlag(int mask) 144 { 145 updateFlag |= mask; 146 if (bufferTree != null) 147 bufferTree.setUpdateFlag(updateFlag & SIDEBAR_BUFFER_LIST_ALL); 148 } 149 150 public static void setUpdateFlagInAllFrames(int mask) 151 { 152 for (int i = 0; i < Editor.getFrameCount(); i++) { 153 Frame frame = Editor.getFrame(i); 154 Sidebar sidebar = frame.getSidebar(); 155 if (sidebar != null) 156 sidebar.setUpdateFlag(mask); 157 } 158 } 159 160 private void setBottomComponent() 161 { 162 if (!SwingUtilities.isEventDispatchThread()) 163 Debug.bug("Sidebar.setBottomComponent() called from background thread!"); 164 if (bottomComponent != null) { 165 Editor editor = frame.getCurrentEditor(); 166 Buffer buffer = editor.getBuffer(); 167 if (frame.getEditorCount() == 2 && buffer.isTransient()) { 168 editor = frame.getOtherEditor(); 169 buffer = editor.getBuffer(); 170 } 171 if (isTaggable(buffer) && bottomComponent instanceof SidebarTagList) { 172 SidebarTagList list = (SidebarTagList) bottomComponent; 173 list.setEditor(editor); 174 if (list.getBuffer() == buffer) 175 return; 176 } else { 177 View view = editor.getCurrentView(); 178 if (view != null && bottomComponent == view.getSidebarComponent()) 179 return; 180 } 181 bottomComponent = null; 183 bottomPanel.removeAll(); 184 } 185 Debug.assertTrue(bottomComponent == null); 186 final Editor editor = getEditor(); 187 bottomComponent = editor.getMode().getSidebarComponent(editor); 188 if (bottomComponent != null) { 189 JScrollPane scrollPane = new JScrollPane ((JComponent )bottomComponent); 190 if (bottomComponent instanceof SidebarList) 191 scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 192 scrollPane.setAlignmentX(LEFT_ALIGNMENT); 193 scrollPane.setBorder(BorderFactory.createEmptyBorder()); 194 bottomPanel.addScrollPane(scrollPane); 195 bottomPanel.validate(); 196 } else 197 bottomPanel.repaint(); 198 } 199 200 public synchronized void refreshSidebar() 201 { 202 if (updateFlag != 0) { 203 if ((updateFlag & SIDEBAR_BUFFER_LIST_ALL) != 0) { 204 SidebarBufferTree bufferTree = getBufferTree(); 205 if (bufferTree != null && bufferTree != frame.getFocusedComponent()) 206 bufferTree.updateBufferList(); 207 } 208 if (bottomComponent == null || bottomComponent != frame.getFocusedComponent()) { 209 setBottomComponent(); 210 if (bottomComponent != null) { 211 bottomPanel.setLabelText(bottomComponent.getLabelText()); 212 bottomComponent.refresh(); 214 if ((updateFlag & SIDEBAR_POSITION) != 0) 215 bottomComponent.updatePosition(); 216 } 217 } 218 updateFlag = 0; 219 } 220 } 221 222 public static void refreshSidebarInAllFrames() 223 { 224 if (!SwingUtilities.isEventDispatchThread()) 225 Debug.bug("refreshSidebarInAllFrames() called from background thread!"); 226 for (int i = 0; i < Editor.getFrameCount(); i++) { 227 Frame frame = Editor.getFrame(i); 228 Sidebar sidebar = frame.getSidebar(); 229 if (sidebar != null) 230 sidebar.refreshSidebar(); 231 } 232 } 233 } 234 | Popular Tags |