001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019package icy.file.xls;
020
021import icy.util.XLSUtil;
022
023import java.io.File;
024import java.io.IOException;
025
026import jxl.Workbook;
027import jxl.format.Colour;
028import jxl.read.biff.BiffException;
029import jxl.write.Label;
030import jxl.write.Number;
031import jxl.write.WritableCellFormat;
032import jxl.write.WritableImage;
033import jxl.write.WritableSheet;
034import jxl.write.WritableWorkbook;
035import jxl.write.WriteException;
036
037/**
038 * @deprecated Use {@link XLSUtil} instead.
039 */
040@Deprecated
041public class XlsManager
042{
043    WritableSheet excelPage = null;
044    WritableWorkbook excelWorkbook;
045
046    /**
047     * Create a new file, overwritting existing one.
048     * 
049     * @param file
050     * @throws IOException
051     */
052    public XlsManager(File file) throws IOException
053    {
054        excelWorkbook = Workbook.createWorkbook(file);
055        // excelPage = excelWorkbook.createSheet("results", 0);
056    }
057
058    /**
059     * load from existing file
060     * 
061     * @param file
062     * @param load
063     * @throws IOException
064     * @throws BiffException
065     */
066    public XlsManager(File file, boolean load) throws IOException, BiffException
067    {
068        if (load)
069        {
070            if (!file.exists())
071            {
072                excelWorkbook = Workbook.createWorkbook(file);
073            }
074            else
075            {
076                excelWorkbook = Workbook.createWorkbook(file, Workbook.getWorkbook(file));
077            }
078        }
079        // FIXME: no else here. Class should be changed now that it can load data.
080    }
081
082    public XlsManager(String file) throws IOException
083    {
084        this(new File(file));
085    }
086
087    public void SaveAndClose()
088    {
089        try
090        {
091            excelWorkbook.write();
092            excelWorkbook.close();
093        }
094        catch (IOException e)
095        {
096            System.err.println("Error while recording XLS");
097        }
098        catch (WriteException e)
099        {
100            System.err.println("Error while recording XLS");
101            e.printStackTrace();
102        }
103    }
104
105    /**
106     * Create a new page. If the page already exists, add an incremented number for distinction.
107     * 
108     * @param title
109     */
110    public void createNewPage(String title)
111    {
112        boolean ok = false;
113        int counter = 2;
114        String pageName = title;
115        while (!ok)
116        {
117            if (excelWorkbook.getSheet(pageName) == null)
118            {
119                excelPage = excelWorkbook.createSheet(pageName, excelWorkbook.getNumberOfSheets() + 1);
120                ok = true;
121            }
122            pageName = title + " " + counter;
123            counter++;
124        }
125    }
126
127    public void setPageName(String name)
128    {
129        excelPage.setName(name);
130    }
131
132    public void addImage(WritableImage image)
133    {
134        try
135        {
136            excelPage.addImage(image);
137        }
138        catch (Exception e)
139        {
140            System.err.println("Error while writing Xls data (XlsManager.java) File Already open by an other app ?");
141        }
142
143    }
144
145    public void setLabel(int x, int y, String texte, Colour background)
146    // public void setLabel(int x, int y, String texte, Color background)
147    {
148        WritableCellFormat wcf = new WritableCellFormat();
149        try
150        {
151            // Colour colour = Colour.getInternalColour( background.getRGB() );
152            wcf.setBackground(background);
153        }
154        catch (WriteException e1)
155        {
156            e1.printStackTrace();
157        }
158        Label label = new Label(x, y, texte, wcf);
159        try
160        {
161            excelPage.addCell(label);
162        }
163        catch (Exception e)
164        {
165            System.err.println("Error while writing Xls data (XlsManager.java) File Already open by an other app ?");
166        }
167
168    }
169
170    public void setLabel(int x, int y, String texte)
171    {
172        Label label = new Label(x, y, texte);
173        try
174        {
175            excelPage.addCell(label);
176        }
177        catch (Exception e)
178        {
179            System.err.println("Error while writing Xls data (XlsManager.java) File Already open by an other app ?");
180        }
181    }
182
183    public void setNumber(int x, int y, double n, Colour background)
184    {
185        WritableCellFormat wcf = new WritableCellFormat();
186        try
187        {
188            // Colour colour = Colour.getInternalColour( background.getRGB() );
189            // wcf.setBackground( colour );
190            wcf.setBackground(background);
191        }
192        catch (WriteException e1)
193        {
194            e1.printStackTrace();
195        }
196        Number number = new Number(x, y, n, wcf);
197        try
198        {
199            excelPage.addCell(number);
200        }
201        catch (Exception e)
202        {
203            System.err.println("Error while writing Xls data (XlsManager.java) File Already open by an other app ?");
204        }
205    }
206
207    public void setNumber(int x, int y, double n)
208    {
209        Number number = new Number(x, y, n);
210        try
211        {
212            excelPage.addCell(number);
213        }
214        catch (Exception e)
215        {
216            System.err.println("Error while writing Xls data (XlsManager.java) File Already open by an other app ?");
217        }
218    }
219
220    public WritableSheet getExcelPage()
221    {
222        return excelPage;
223    }
224
225}