View Javadoc

1   /*
2    * This file is part of NetPisteur.
3    * 
4    * Copyright 2001-2002 : Olivier CORNEC, David BOUANCHEAU, Loic LOPEZ,
5    *                       Gaetan BOUDARD, Mael LE LANNOU, Julien ROBINEAU
6    * 
7    * Copyright 2002-2003 : Olivier BRIENS, Simon DEZE, Florence FRIGOULT,
8    *                       Olivier JOURNEAULT, Francois MARTINIER, Damien RAUDE-MORVAN
9    * 
10   * Copyright 2004-2007 : Damien RAUDE-MORVAN
11   * 
12   * NetPisteur is free software; you can redistribute it and/or modify
13   * it under the terms of the GNU General Public License as published by
14   * the Free Software Foundation; either version 2 of the License, or
15   * (at your option) any later version.
16   *
17   * NetPisteur is distributed in the hope that it will be useful,
18   * but WITHOUT ANY WARRANTY; without even the implied warranty of
19   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20   * GNU General Public License for more details.
21   *
22   * You should have received a copy of the GNU General Public License
23   * along with NetPisteur; if not, write to the Free Software
24   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
25   *
26   */
27  package com.drazzib.netpisteur.monitor;
28  
29  import java.io.BufferedWriter;
30  import java.io.FileWriter;
31  import java.io.IOException;
32  import java.io.PrintWriter;
33  import java.io.WriteAbortedException;
34  import java.util.ArrayList;
35  
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  
39  /**
40   * Represents the list of the placements of the mouse
41   * 
42   * @author NetPisteur Team
43   */
44  
45  public class ListPlacement {
46  	
47  	private static Log log = LogFactory.getLog(ListPlacement.class);
48  
49  	// /////////////////////////////////////
50  	// attributes
51  
52  	// Vector of mouse's positions
53  	private ArrayList lesPlacements;
54  
55  	// number of elements
56  	private int nbrePlacement;
57  
58  	// /////////////////////////////////////
59  	// Constructor
60  
61  	public ListPlacement() {
62  		this.lesPlacements = new ArrayList();
63  		this.nbrePlacement = 0;
64  	} // ListePlacement()
65  
66  	// Accessor
67  
68  	/**
69  	 * add a new object Placement in the array
70  	 * 
71  	 * @param placCourant :
72  	 *            object Placement which you want to add
73  	 */
74  	public void addPlacement(Placement placCourant) {
75  		this.lesPlacements.add(this.nbrePlacement, placCourant);
76  		this.nbrePlacement++;
77  	} // addPlacement()
78  
79  	/**
80  	 * give the object Placement corresponding with the index of array. if the
81  	 * object doesn't exist, the method return a null object
82  	 * 
83  	 * @param index :
84  	 *            index of the array
85  	 * 
86  	 * @return object Placement which has been found
87  	 */
88  	public Placement getPlacement(int index) {
89  		Placement lePlacement;
90  		if (index >= this.nbrePlacement) {
91  			// :FIXME:
92  			throw new RuntimeException("Any placement found\n");
93  		}
94  		lePlacement = (Placement) this.lesPlacements.get(index);
95  
96  		return lePlacement;
97  	} // getPlacement()
98  
99  	/**
100 	 * writeListePlacementToDisk() : write all the informations wich the array
101 	 * has compil:
102 	 * 
103 	 * @return boolean (true if there is no problem with the write. else it's
104 	 *         false )
105 	 * 
106 	 */
107 	public boolean writeListePlacementToDisk(String fic, String sep) {
108 		int ord, abs;
109 		Placement lePlacement;
110 		PrintWriter out;
111 		String file = fic;
112 		boolean retour = false;
113 
114 		try {
115 
116 			out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
117 			if (this.nbrePlacement <= 0) {
118 				out.println("\nAucun dplacements de la souris\n");
119 
120 			}// if
121 			else {
122 				out.println("# Liste des placements de la souris : ");
123 				for (int i = 0; i < this.nbrePlacement; i++) {
124 					lePlacement = (Placement) this.lesPlacements.get(i);
125 					abs = lePlacement.getAbs();
126 					ord = lePlacement.getOrd();
127 					out.println(abs + "x" + ord + sep + lePlacement.getHours()
128 							+ ":" + lePlacement.getMinutes() + ":"
129 							+ lePlacement.getSeconds() + "");
130 				}// else
131 
132 			}
133 			// out.println("------------------------------------");
134 
135 			out.close();
136 			retour = true;
137 
138 		} catch (WriteAbortedException e) {
139 			log.error("Erreur d'écriture du fichier", e);
140 
141 		} catch (IOException e) {
142 			log.error("Erreur physique de lecture", e);
143 
144 		}
145 
146 		return (retour);
147 	} // ----------------------------------------------------writeListePlacementToDisk()
148 
149 } // class ListPlacement
150