1 22 23 package org.gjt.sp.jedit.gui; 24 25 import java.awt.*; 27 import java.awt.event.*; 28 import javax.swing.*; 29 import javax.swing.border.EmptyBorder ; 30 import javax.swing.event.*; 31 import org.gjt.sp.jedit.*; 32 import org.gjt.sp.jedit.msg.PropertiesChanged; 33 import org.gjt.sp.util.Log; 34 36 public class LogViewer extends JPanel implements DefaultFocusComponent, 37 EBComponent 38 { 39 public LogViewer() 41 { 42 super(new BorderLayout()); 43 44 JPanel caption = new JPanel(); 45 caption.setLayout(new BoxLayout(caption,BoxLayout.X_AXIS)); 46 caption.setBorder(new EmptyBorder (6,6,6,6)); 47 48 String settingsDirectory = jEdit.getSettingsDirectory(); 49 if(settingsDirectory != null) 50 { 51 String [] args = { MiscUtilities.constructPath( 52 settingsDirectory, "activity.log") }; 53 JLabel label = new JLabel(jEdit.getProperty( 54 "log-viewer.caption",args)); 55 caption.add(label); 56 } 57 58 caption.add(Box.createHorizontalGlue()); 59 60 tailIsOn = jEdit.getBooleanProperty("log-viewer.tail", false); 61 tail = new JCheckBox( 62 jEdit.getProperty("log-viewer.tail.label"),tailIsOn); 63 tail.addActionListener(new ActionHandler()); 64 caption.add(tail); 65 66 caption.add(Box.createHorizontalStrut(12)); 67 68 copy = new JButton(jEdit.getProperty("log-viewer.copy")); 69 copy.addActionListener(new ActionHandler()); 70 caption.add(copy); 71 72 ListModel model = Log.getLogListModel(); 73 model.addListDataListener(new ListHandler()); 74 list = new LogList(model); 75 76 add(BorderLayout.NORTH,caption); 77 JScrollPane scroller = new JScrollPane(list); 78 Dimension dim = scroller.getPreferredSize(); 79 dim.width = Math.min(600,dim.width); 80 scroller.setPreferredSize(dim); 81 add(BorderLayout.CENTER,scroller); 82 83 propertiesChanged(); 84 } 86 public void handleMessage(EBMessage msg) 88 { 89 if(msg instanceof PropertiesChanged) 90 propertiesChanged(); 91 } 93 public void addNotify() 95 { 96 super.addNotify(); 97 if(tailIsOn) 98 { 99 int index = list.getModel().getSize() - 1; 100 list.ensureIndexIsVisible(index); 101 } 102 103 EditBus.addToBus(this); 104 } 106 public void removeNotify() 108 { 109 super.removeNotify(); 110 111 EditBus.removeFromBus(this); 112 } 114 public void focusOnDefaultComponent() 116 { 117 list.requestFocus(); 118 } 120 private JList list; 122 private JButton copy; 123 private JCheckBox tail; 124 private boolean tailIsOn; 125 126 private void propertiesChanged() 128 { 129 list.setFont(jEdit.getFontProperty("view.font")); 130 list.setFixedCellHeight(list.getFontMetrics(list.getFont()) 131 .getHeight()); 132 } 134 136 class ActionHandler implements ActionListener 138 { 139 public void actionPerformed(ActionEvent e) 140 { 141 Object src = e.getSource(); 142 if(src == tail) 143 { 144 tailIsOn = !tailIsOn; 145 jEdit.setBooleanProperty("log-viewer.tail",tailIsOn); 146 if(tailIsOn) 147 { 148 int index = list.getModel().getSize(); 149 if(index != 0) 150 { 151 list.ensureIndexIsVisible(index - 1); 152 } 153 } 154 } 155 else if(src == copy) 156 { 157 StringBuffer buf = new StringBuffer (); 158 Object [] selected = list.getSelectedValues(); 159 if(selected != null && selected.length != 0) 160 { 161 for(int i = 0; i < selected.length; i++) 162 { 163 buf.append(selected[i]); 164 buf.append('\n'); 165 } 166 } 167 else 168 { 169 ListModel model = list.getModel(); 170 for(int i = 0; i < model.getSize(); i++) 171 { 172 buf.append(model.getElementAt(i)); 173 buf.append('\n'); 174 } 175 } 176 Registers.setRegister('$',buf.toString()); 177 } 178 } 179 } 181 class ListHandler implements ListDataListener 183 { 184 public void intervalAdded(ListDataEvent e) 185 { 186 contentsChanged(e); 187 } 188 189 public void intervalRemoved(ListDataEvent e) 190 { 191 contentsChanged(e); 192 } 193 194 public void contentsChanged(ListDataEvent e) 195 { 196 if(tailIsOn) 197 { 198 SwingUtilities.invokeLater(new Runnable () 199 { 200 public void run() 201 { 202 int index = list.getModel().getSize() - 1; 203 list.ensureIndexIsVisible(index); 204 } 205 }); 206 } 207 } 208 } 210 class LogList extends JList 212 { 213 LogList(ListModel model) 214 { 215 super(model); 216 setVisibleRowCount(24); 217 getSelectionModel().setSelectionMode( 218 ListSelectionModel.SINGLE_INTERVAL_SELECTION); 219 setAutoscrolls(true); 220 } 221 222 public void processMouseEvent(MouseEvent evt) 223 { 224 if(evt.getID() == MouseEvent.MOUSE_PRESSED) 225 { 226 startIndex = list.locationToIndex( 227 evt.getPoint()); 228 } 229 super.processMouseEvent(evt); 230 } 231 232 public void processMouseMotionEvent(MouseEvent evt) 233 { 234 if(evt.getID() == MouseEvent.MOUSE_DRAGGED) 235 { 236 int row = list.locationToIndex(evt.getPoint()); 237 if(row != -1) 238 { 239 if(startIndex == -1) 240 { 241 list.setSelectionInterval(row,row); 242 startIndex = row; 243 } 244 else 245 list.setSelectionInterval(startIndex,row); 246 list.ensureIndexIsVisible(row); 247 evt.consume(); 248 } 249 } 250 else 251 super.processMouseMotionEvent(evt); 252 } 253 254 private int startIndex; 255 } } 257 | Popular Tags |