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.form;
034:
035: import java.io.IOException;
036: import java.io.ObjectInputStream;
037: import java.io.ObjectOutputStream;
038: import java.io.Serializable;
039:
040: /**
041: * Artenum Contribution
042: *
043: * @author <a href="mailto:jourdain@artenum.com">Sebastien Jourdain</a> - <a
044: * href="http://www.artenum.com">Artenum</a>
045: */
046: public class Option implements Serializable {
047: private static final long serialVersionUID = 1L;
048: private static final String SELECTED_KEY = "[selected]";
049: private static final String EMPTY_NAME = "(empty)";
050: private String label;
051: private boolean selected;
052:
053: public Option() {
054: this (EMPTY_NAME, false);
055: }
056:
057: public Option(String inputLine) {
058: int selectedIndex = inputLine.indexOf(SELECTED_KEY);
059:
060: if (selectedIndex != -1) {
061: label = inputLine.substring(0, selectedIndex);
062: selected = true;
063: } else {
064: label = inputLine;
065: selected = false;
066: }
067:
068: if (label.length() == 0) {
069: label = EMPTY_NAME;
070: }
071:
072: if (label.indexOf("\n") != -1) {
073: label = label.substring(0, label.indexOf("\n"));
074:
075: // System.out.println("Supprime retour: " + label);
076: } else {
077: // System.out.println("pas retour: " + label);
078: }
079:
080: label = label.trim();
081: }
082:
083: public Option(String label, boolean selected) {
084: this .label = label;
085: this .selected = selected;
086: }
087:
088: public String getLabel() {
089: return label;
090: }
091:
092: public void setLabel(String label) {
093: this .label = label;
094: }
095:
096: public boolean isSelected() {
097: return selected;
098: }
099:
100: public void setSelected(boolean selected) {
101: this .selected = selected;
102: }
103:
104: public String toString() {
105: return label + (selected ? SELECTED_KEY : "") + "\n";
106: }
107:
108: private void writeObject(ObjectOutputStream out) throws IOException {
109: // Write current version
110: out.writeInt(1);
111:
112: // Write data for V1
113: out.writeObject(label);
114: out.writeBoolean(selected);
115:
116: // Write more data for V2
117: }
118:
119: private void readObject(ObjectInputStream in) throws IOException,
120: ClassNotFoundException {
121: int version = in.readInt();
122:
123: switch (version) {
124: case 1:
125: label = (String) in.readObject();
126: selected = in.readBoolean();
127:
128: break;
129: }
130: }
131: }
|