Commits

Anonymous committed 5521a21

Added file filtering.

Comments (0)

Files changed (3)

tailog/src/magmax/tailog/gui/MainWindow.java

 import javax.swing.JSplitPane;
 import javax.swing.JTable;
 import javax.swing.JTree;
+import javax.swing.ListSelectionModel;
+import javax.swing.event.ListSelectionEvent;
+import javax.swing.event.ListSelectionListener;
 
 public class MainWindow {
 
 	private JTable mainTable;
 	private ModelChangedLines mainModel;
 	private ModelFileTree modelFiles;
-	private JTable treeFiles;
+	private JTable tableFiles;
 
 	private MainWindow() {
 		createMainFrame();
 
 	private void createDesktop() {
 		JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
+		split.setDividerLocation(150);
 		
-		createFileTable ();
 		createMainTable();
-		
+		createFileTable();
+
 		JScrollPane scrollPanelMain = new JScrollPane(mainTable);
-		JScrollPane scrollPanelFiles = new JScrollPane(treeFiles);
-		
-		split.add (scrollPanelFiles);
+		JScrollPane scrollPanelFiles = new JScrollPane(tableFiles);
+
+		split.add(scrollPanelFiles);
 		split.add(scrollPanelMain);
 
-		
 		mainFrame.getContentPane().add(split, BorderLayout.CENTER);
 		mainFrame.pack();
 	}
-	
-	private void createFileTable ()
-	{
+
+	private void createFileTable() {
 		modelFiles = new ModelFileTree();
-		treeFiles = new JTable(modelFiles);
-		treeFiles.setVisible(true);
+		tableFiles = new JTable(modelFiles);
+		tableFiles.setVisible(true);
+
+		ListSelectionModel selectionModel = tableFiles.getSelectionModel();
+		selectionModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
+		
+		selectionModel.addListSelectionListener(new ListSelectionListener() {
+			
+			@Override
+			public void valueChanged(ListSelectionEvent e) {
+				ListSelectionModel selectionModel = tableFiles.getSelectionModel();
+				for ( int i = e.getFirstIndex(); i <= e.getLastIndex(); ++i)
+					if ( selectionModel.isSelectedIndex(i))
+						mainModel.show (modelFiles.getFile(i));
+					else
+						mainModel.hide (modelFiles.getFile(i));
+			}
+		});
+		
 	}
-	
-	private void createMainTable ()
-	{
+
+	private void createMainTable() {
 		mainModel = new ModelChangedLines();
 		mainTable = new JTable(mainModel);
 		mainTable.setPreferredSize(new Dimension(600, 400));
 		mainFrame.pack();
 	}
 
-
 	class MenuBar extends JMenuBar {
 		private static final long serialVersionUID = 1L;
 
 		private JMenu getMenuEvents() {
 			JMenu result = new JMenu("Events");
 
-			JMenuItem dateMark = new JMenuItem("Set_Date_Mark");
+			JMenuItem dateMark = new JMenuItem("Set Date Mark");
 			dateMark.setAction(new SetDateMarkAction());
 			result.add(dateMark);
-			
+
 			JSeparator sep = new JSeparator();
 			result.add(sep);
 
 		private class ClearAction extends AbstractAction {
 			private static final long serialVersionUID = 1L;
 
+			public ClearAction (){
+				super("Clear");
+			}
 			@Override
 			public void actionPerformed(ActionEvent e) {
 				mainModel.clear();
 			}
 		}
 
-		
 		private class SetDateMarkAction extends AbstractAction {
 			private static final long serialVersionUID = 1L;
 
+			public SetDateMarkAction (){
+				super("Set Date Mark");
+			}
+			
 			@Override
 			public void actionPerformed(ActionEvent e) {
 				mainModel.addDateLine();
 					try {
 						modelFiles.addFile(file);
 						mainModel.addFile(file);
+						int rows = modelFiles.getRowCount() - 1;
+						tableFiles.getSelectionModel().addSelectionInterval(rows, rows);
 					} catch (IOException e1) {
 						// TODO Auto-generated catch block
 						e1.printStackTrace();

tailog/src/magmax/tailog/gui/ModelChangedLines.java

 public class ModelChangedLines extends AbstractTableModel implements Observer {
 	private static final long serialVersionUID = 1L;
 	private ArrayList<ModelLine> lines = new ArrayList<ModelLine>();
+	private ArrayList<ModelLine> linesShowed = new ArrayList<ModelLine>();
+	private ArrayList<File> filesShowed = new ArrayList<File>();
 	private String[] headers = new String[] { "File", "Log Line" };
 
 	@Override
 
 	@Override
 	public int getRowCount() {
-		return lines.size();
+		return linesShowed.size();
 	}
 
 	@Override
 	public Object getValueAt(int rowIndex, int columnIndex) {
-		ModelLine line = lines.get(rowIndex);
+		ModelLine line = linesShowed.get(rowIndex);
 		switch (columnIndex) {
 		case 0:
-			return line.filename;
+			return line.getFilename();
 		case 1:
 			return line.line;
 		}
 	@Override
 	public synchronized void update(Observable o, Object arg) {
 		FileWatcher watcher = (FileWatcher) o;
-		int size = lines.size();
-		lines.add(new ModelLine(watcher.getFile().getName(), arg.toString()));
-		fireTableRowsInserted(size, size);
+		File file = watcher.getFile();
+		ModelLine line = new ModelLine(file, arg.toString());
+		lines.add(line);
+		if (filesShowed.contains(file)) {
+			int size = linesShowed.size();
+			linesShowed.add(line);
+			fireTableRowsInserted(size, size);
+		}
 	}
 
 	public synchronized void addDateLine() {
 		Calendar calendar = Calendar.getInstance();
-		int size = lines.size();
-		lines.add(new ModelLine("", "------  " + calendar.getTime()
-				+ "  ------"));
+		int size = linesShowed.size();
+		ModelLine line = new ModelLine(null, "------  " + calendar.getTime()
+				+ "  ------");
+		lines.add(line);
+		linesShowed.add(line);
 		fireTableRowsInserted(size, size);
 	}
 
 	public void clear() {
 		lines.clear();
+		linesShowed.clear();
 		fireTableDataChanged();
 	}
 
 	}
 
 	private class ModelLine {
-		public String filename = "";
+		public File file = null;
 		public String line = "";
 
-		public ModelLine(String filename, String line) {
+		public ModelLine(File file, String line) {
 			super();
-			this.filename = filename;
+			this.file = file;
 			this.line = line;
 		}
+
+		public Object getFilename() {
+			if (file == null)
+				return null;
+			return file.getName();
+		}
+	}
+
+	public synchronized void show(File file) {
+		if (filesShowed.contains(file))
+			return;
+		filesShowed.add(file);
+		updateShownLines();
+	}
+
+	public synchronized void hide(File file) {
+		if (!filesShowed.contains(file))
+			return;
+		filesShowed.remove(file);
+		updateShownLines();
+	}
+
+	private void updateShownLines() {
+		linesShowed.clear();
+		for (ModelLine each : lines) {
+			if ( each.file == null || filesShowed.contains(each.file))
+				linesShowed.add(each);
+		}
+		fireTableDataChanged();
 	}
 }

tailog/src/magmax/tailog/gui/ModelFileTree.java

 		list.remove(rowIndex);
 		fireTableRowsDeleted(rowIndex, rowIndex);
 	}
+
+	public File getFile(int rowIndex) {
+		return list.get(rowIndex);
+	}
 }