001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.so6.core.engine;
034:
035: import java.awt.BorderLayout;
036: import java.awt.FlowLayout;
037: import java.awt.event.ActionEvent;
038: import java.awt.event.ActionListener;
039:
040: import java.io.BufferedReader;
041: import java.io.File;
042: import java.io.FileReader;
043: import java.io.FileWriter;
044:
045: import java.util.Enumeration;
046: import java.util.Vector;
047: import java.util.logging.Logger;
048:
049: import javax.swing.AbstractListModel;
050: import javax.swing.ImageIcon;
051: import javax.swing.JButton;
052: import javax.swing.JComponent;
053: import javax.swing.JLabel;
054: import javax.swing.JList;
055: import javax.swing.JPanel;
056: import javax.swing.JScrollPane;
057: import javax.swing.JTextField;
058: import javax.swing.ListCellRenderer;
059:
060: public class Ignore {
061: public static String ignoreFile = ".ignore.so6";
062: private String root;
063: private String ignorePath;
064: private Vector ignore = new Vector();
065: private IgnoreModel ignoreModel = new IgnoreModel();
066:
067: public Ignore(String pathReplicateRoot, String ignorePath) {
068: try {
069: this .root = (new File(pathReplicateRoot)).getAbsolutePath();
070: this .ignorePath = ignorePath;
071: this .load();
072: } catch (Exception ce) {
073: Logger.getLogger("ui.log").finest(
074: "Ignore:" + ce.getMessage());
075: }
076: }
077:
078: // add a new entry to ignore...
079: public void addIgnore(String regexp) {
080: ignore.addElement(regexp);
081:
082: try {
083: this .save();
084: ignoreModel.setChanged();
085: } catch (Exception e) {
086: e.printStackTrace();
087: }
088: }
089:
090: public void removeIgnore(int index) {
091: ignore.removeElementAt(index);
092:
093: try {
094: this .save();
095: ignoreModel.setChanged();
096: } catch (Exception e) {
097: e.printStackTrace();
098: }
099: }
100:
101: public boolean match(File dir, String name) {
102: String path = dir.getAbsolutePath() + File.separator + name;
103: String rpath = path.substring(path.lastIndexOf(root)
104: + root.length());
105: rpath = rpath.replace(File.separatorChar, ':');
106:
107: for (Enumeration e = ignore.elements(); e.hasMoreElements();) {
108: String ign = (String) e.nextElement();
109:
110: if (rpath.matches(ign)) {
111: return true;
112: }
113: }
114:
115: return false;
116: }
117:
118: public boolean match(String path) {
119: String localPath = path.replace(File.separatorChar, ':');
120:
121: for (Enumeration e = ignore.elements(); e.hasMoreElements();) {
122: String ign = (String) e.nextElement();
123:
124: if (localPath.matches(ign)) {
125: return true;
126: }
127: }
128:
129: return false;
130: }
131:
132: public void load() throws Exception {
133: File f = new File(ignorePath);
134:
135: if (!(f.exists())) {
136: throw new Exception(f.getPath() + " does not exists");
137: }
138:
139: try {
140: ignore.clear();
141:
142: String tmp;
143: BufferedReader in = new BufferedReader(new FileReader(
144: ignorePath));
145:
146: while ((tmp = in.readLine()) != null)
147: ignore.add(tmp);
148:
149: in.close();
150: in = null;
151: f = null;
152: } catch (Exception e) {
153: throw new Exception(e.getMessage());
154: }
155: }
156:
157: public void save() throws Exception {
158: File f = new File(ignorePath);
159:
160: if (f.exists()) {
161: if (!(f.delete())) {
162: throw new Exception("Cannot delete:" + ignorePath);
163: }
164: }
165:
166: try {
167: FileWriter file = new FileWriter(ignorePath);
168: Vector v = new Vector(ignore);
169:
170: while (!v.isEmpty()) {
171: file.write((String) v.remove(0) + "\n");
172: }
173:
174: file.close();
175: file = null;
176: } catch (Exception e) {
177: e.printStackTrace();
178: throw new Exception("Cannot ignore:" + e.getMessage());
179: }
180: }
181:
182: // UI Part now...
183: public JComponent getComponent() {
184: return new IgnorePanel();
185: }
186:
187: public class IgnoreModel extends AbstractListModel {
188: public Object getElementAt(int index) {
189: return ignore.elementAt(index);
190: }
191:
192: public void setRemoved() {
193: fireIntervalRemoved(this , 0, ignore.size());
194: }
195:
196: public void setChanged() {
197: fireContentsChanged(this , 0, ignore.size());
198: }
199:
200: public int getSize() {
201: return ignore.size();
202: }
203: }
204:
205: class IgnorePanel extends JPanel {
206: private JList jlist = new JList(ignoreModel);
207:
208: public IgnorePanel() {
209: //jlist.setCellRenderer(new MyCellRenderer());
210: this .setLayout(new BorderLayout());
211:
212: JScrollPane scrollList = new JScrollPane(jlist);
213: final JTextField add = new JTextField(25);
214: add.addActionListener(new ActionListener() {
215: public void actionPerformed(ActionEvent e) {
216: Ignore.this .addIgnore(add.getText());
217: }
218: });
219:
220: JButton remove = new JButton("Remove");
221: remove.addActionListener(new ActionListener() {
222: public void actionPerformed(ActionEvent e) {
223: final int index = jlist.getSelectedIndex();
224:
225: if (index >= 0) {
226: Ignore.this .removeIgnore(index);
227: }
228: }
229: });
230:
231: JPanel control = new JPanel(new FlowLayout());
232: control.add(remove);
233: this .add(BorderLayout.NORTH, add);
234: this .add(BorderLayout.EAST, control);
235: this .add(BorderLayout.CENTER, scrollList);
236: }
237:
238: class MyCellRenderer extends JLabel implements ListCellRenderer {
239: private ImageIcon regexpIcon;
240:
241: MyCellRenderer() {
242: ClassLoader cl = this .getClass().getClassLoader();
243: regexpIcon = new ImageIcon(cl
244: .getResource("icons/GreenFlag.gif"));
245: }
246:
247: // This is the only method defined by ListCellRenderer.
248: // We just reconfigure the JLabel each time we're called.
249: public java.awt.Component getListCellRendererComponent(
250: JList list, Object value, int index,
251: boolean isSelected, boolean cellHasFocus) {
252: String regexp = (String) value;
253: setIcon(regexpIcon);
254: setText(regexp);
255:
256: if (isSelected) {
257: setBackground(list.getSelectionBackground());
258: setForeground(list.getSelectionForeground());
259: } else {
260: setBackground(list.getBackground());
261: setForeground(list.getForeground());
262: }
263:
264: setEnabled(list.isEnabled());
265: setFont(list.getFont());
266: setOpaque(true);
267:
268: return this;
269: }
270: }
271: }
272: }
|