Révision 1496

tmp/org.txm.rcp/src/main/java/org/txm/rcp/ApplicationWorkbenchAdvisor.java (revision 1496)
260 260
						// restore corpora if TXMHOME has been created
261 261
						if (txmHomeRestored) {
262 262
							//createBackUpDirectory(monitor);
263
							System.out.println("Installing sample corpus...");
263
							
264 264
							installCorporaFromSamplesDirectory(monitor);
265 265
//							this.syncExec(new Runnable() {
266 266
//								@Override
......
853 853
		
854 854
		// load corpora from the install directory
855 855
		if (sampleCorporaDirectory.exists()) { //$NON-NLS-1$
856
			
856
			System.out.println("Installing sample corpus...");
857 857
			LoadBinaryCorporaDirectory.loadBinaryCorpusFromCorporaDirectory(sampleCorporaDirectory);
858 858
//			Log.info(TXMUIMessages.ApplicationWorkbenchAdvisor_11);
859 859
//			monitor.setTaskName(TXMUIMessages.ApplicationWorkbenchAdvisor_11);
tmp/org.txm.lexicaltable.rcp/src/org/txm/lexicaltable/rcp/editors/LexicalTableEditor.java (revision 1496)
633 633
	 */
634 634
	 @Deprecated
635 635
	public void exportData(File file) {
636
		 lexicalTable.getData().exportData(file, "\t", ""); //$NON-NLS-1$ //$NON-NLS-2$
636
		 lexicalTable.getData().toTxt(file, "UTF-8", "\t", ""); //$NON-NLS-1$ //$NON-NLS-2$
637 637
	}
638 638

  
639 639
	/**
......
644 644
	 @Deprecated
645 645
	public void importData(File file) {
646 646
		try {
647
			
647 648
			if (lexicalTable.getData().importData(file)) {
649
				lexicalTable.setAltered();
648 650
				// fix col names
649 651
				String[] colnames = lexicalTable.getColNames().asStringsArray();
650 652
				TableColumn[] cols = viewer.getTable().getColumns();
651
				if (colnames.length > cols.length) {
652
					Log.severe(LexicalTableUIMessages.bind(LexicalTableUIMessages.errorColonDifferentColumnsNumberColonBeforeP0AfterP1, Arrays.toString(cols), Arrays.toString(colnames)));
653
				int ncol = cols.length - 3;
654
				if (colnames.length != ncol) {
655
					//Log.severe(LexicalTableUIMessages.bind(LexicalTableUIMessages.errorColonDifferentColumnsNumberColonBeforeP0AfterP1, ncol, colnames.length));
656
					LexicalTable r = getResult();
657
					close();
658
					TXMEditor.openEditor(r, LexicalTableEditor.ID);
653 659
					return;
654 660
				}
655 661
				for (int i = 0; i < colnames.length ; i++) {
tmp/org.txm.lexicaltable.rcp/src/org/txm/lexicaltable/rcp/handlers/ImportTable.java (revision 1496)
32 32
import org.eclipse.core.commands.AbstractHandler;
33 33
import org.eclipse.core.commands.ExecutionEvent;
34 34
import org.eclipse.core.commands.ExecutionException;
35
import org.eclipse.osgi.util.NLS;
35 36
import org.eclipse.swt.SWT;
36 37
import org.eclipse.swt.widgets.FileDialog;
37 38
import org.eclipse.swt.widgets.Shell;
38 39
import org.eclipse.ui.IWorkbenchPart;
39 40
import org.eclipse.ui.handlers.HandlerUtil;
41
import org.rosuda.REngine.REXPMismatchException;
42
import org.txm.lexicaltable.core.functions.LexicalTable;
40 43
import org.txm.lexicaltable.rcp.editors.LexicalTableEditor;
44
import org.txm.rcp.handlers.BaseAbstractHandler;
41 45
import org.txm.rcp.swt.dialog.LastOpened;
46
import org.txm.statsengine.r.core.exceptions.RWorkspaceException;
47
import org.txm.utils.logger.Log;
42 48

  
43 49
// TODO: Auto-generated Javadoc
44 50
/**
45 51
 * The Class ImportTable.
46 52
 */
47
public class ImportTable extends AbstractHandler {
53
public class ImportTable extends BaseAbstractHandler {
48 54

  
49 55
	private static final String ID = "org.txm.rcp.commands.editor.ImportTable"; //$NON-NLS-1$
50 56
	/* (non-Javadoc)
......
54 60
	public Object execute(ExecutionEvent event) throws ExecutionException {
55 61
		IWorkbenchPart editor = HandlerUtil.getActiveWorkbenchWindow(event)
56 62
				.getActivePage().getActivePart();
63
		
64
		Object sel = this.getCorporaViewSelectedObject(event);
65
		
57 66
		if (editor instanceof LexicalTableEditor) {
58 67
			LexicalTableEditor LTeditor = (LexicalTableEditor) editor;
59 68
			Shell shell = HandlerUtil.getActiveWorkbenchWindowChecked(event)
......
67 76
			if (path != null) {
68 77
				File file = new File(path);
69 78
				LastOpened.set(ID, file.getParent(), file.getName());
70
				if (file.getParentFile().exists())
79
				if (file.exists()) {
71 80
					LTeditor.importData(file);
81
				}
72 82
			}
83
		} else if (sel instanceof LexicalTable) {
84
			LexicalTable lt = (LexicalTable)sel;
85
			Shell shell = HandlerUtil.getActiveWorkbenchWindowChecked(event)
86
					.getShell();
87
			FileDialog d = new FileDialog(shell, SWT.OPEN);
88
			if (LastOpened.getFile(ID) != null) {
89
				d.setFilterPath(LastOpened.getFolder(ID));
90
				d.setFileName(LastOpened.getFile(ID));
91
			}
92
			String path = d.open();
93
			if (path != null) {
94
				File file = new File(path);
95
				LastOpened.set(ID, file.getParent(), file.getName());
96
				if (file.exists()) {
97
					try {
98
						lt.compute();
99
						lt.getData().importData(file);
100
						lt.setAltered();
101
						System.out.println(NLS.bind("Data imported: {0} columns and {1} rows.",lt.getNColumns(), lt.getNRows()));
102
					} catch (Exception e) {
103
						System.out.println("Error: data import failed: "+e);
104
						Log.printStackTrace(e);
105
					}
106
				}
107
			}
73 108
		}
74 109
		return event;
75 110
	}
tmp/org.txm.lexicaltable.rcp/plugin.xml (revision 1496)
33 33
               </or>
34 34
            </visibleWhen>
35 35
         </command>
36
         <command
37
               commandId="org.txm.lexicaltable.rcp.handlers.ImportTable"
38
               style="push">
39
            <visibleWhen
40
                  checkEnabled="false">
41
               <reference
42
                     definitionId="OneLexicalTableSelected">
43
               </reference>
44
            </visibleWhen>
45
         </command>
36 46
      </menuContribution>
37 47
      <menuContribution
38 48
            locationURI="menu:menu.corpus">
......
55 65
               </or>
56 66
            </visibleWhen>
57 67
         </command>
68
         <command
69
               commandId="org.txm.lexicaltable.rcp.handlers.ImportTable"
70
               style="push">
71
            <visibleWhen
72
                  checkEnabled="false">
73
               <reference
74
                     definitionId="OneLexicalTableSelected">
75
               </reference>
76
            </visibleWhen>
77
         </command>
58 78
      </menuContribution>
59 79
      <menuContribution
60 80
            allPopups="false"
......
76 96
               </or>
77 97
            </visibleWhen>
78 98
         </command>
99
         <command
100
               commandId="org.txm.lexicaltable.rcp.handlers.ImportTable"
101
               style="push">
102
            <visibleWhen
103
                  checkEnabled="false">
104
               <reference
105
                     definitionId="OneLexicalTableSelected">
106
               </reference>
107
            </visibleWhen>
108
         </command>
79 109
      </menuContribution>
80 110
      <menuContribution
81 111
            allPopups="false"
......
111 141
               visible="true">
112 142
         </separator>
113 143
         <command
114
               commandId="org.txm.lexicaltable.rcp.handlers.ExportLexicalTable"
115
               style="push">
116
         </command>
117
         <command
118 144
               commandId="org.txm.lexicaltable.rcp.handlers.ImportTable"
119 145
               style="push">
120 146
         </command>
......
148 174
            name="%command.name.38">
149 175
      </command>
150 176
      <command
151
            defaultHandler="org.txm.lexicaltable.rcp.handlers.ExportLexicalTable"
152
            id="org.txm.lexicaltable.rcp.handlers.ExportLexicalTable"
153
            name="%command.name.39">
154
      </command>
155
      <command
156 177
            defaultHandler="org.txm.lexicaltable.rcp.handlers.ImportTable"
157 178
            id="org.txm.lexicaltable.rcp.handlers.ImportTable"
158 179
            name="%command.name.40">
tmp/org.txm.statsengine.r.core/src/org/txm/statsengine/r/core/data/MatrixImpl.java (revision 1496)
27 27
//
28 28
package org.txm.statsengine.r.core.data;
29 29

  
30
import java.io.BufferedReader;
31 30
import java.io.File;
32
import java.io.FileInputStream;
33
import java.io.InputStreamReader;
34 31
import java.util.ArrayList;
35 32
import java.util.Arrays;
36 33
import java.util.Collections;
......
38 35

  
39 36
import org.rosuda.REngine.REXP;
40 37
import org.rosuda.REngine.REXPMismatchException;
41
import org.txm.core.messages.TXMCoreMessages;
42 38
import org.txm.statsengine.core.StatException;
43 39
import org.txm.statsengine.core.data.Matrix;
44 40
import org.txm.statsengine.core.data.Vector;
......
47 43
import org.txm.statsengine.r.core.RWorkspace;
48 44
import org.txm.statsengine.r.core.exceptions.RException;
49 45
import org.txm.statsengine.r.core.exceptions.RWorkspaceException;
50
import org.txm.utils.CharsetDetector;
51 46
import org.txm.utils.OSDetector;
52
import org.txm.utils.io.IOUtils;
53 47
import org.txm.utils.logger.Log;
54 48

  
55 49
import cern.colt.matrix.DoubleMatrix2D;
56 50

  
57
// TODO: Auto-generated Javadoc
58 51
/**
59 52
 * Implementation of the {@link Matrix} interface, wrapping a R matrix.
60 53
 * 
......
611 604

  
612 605
		String path = f.getAbsolutePath();
613 606

  
614
		CharsetDetector guesser = new CharsetDetector(f);
615
		if (!(IOUtils.UTF8.equals(guesser.getEncoding().toUpperCase()))) {
616
			System.out.println(TXMCoreMessages.bind("Warning: file encoding seems to not be 'UTF-8' but '{0}'", guesser.getEncoding()));
617
		}
618

  
619
		int nline = 0;
620
		try {
621
			BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f), IOUtils.UTF8));
622
			while(reader.readLine() != null) nline++;
623
			reader.close();
624
		} catch(Exception e) { }
625

  
626
		if (nline == 0) {
627
			Log.severe(TXMCoreMessages.bind("Error: the file {0} is empty.", f));
628
			return false;
629
		}
630

  
631
		if (OSDetector.isFamilyWindows()) //$NON-NLS-1$ //$NON-NLS-2$
607
		if (OSDetector.isFamilyWindows()) {
632 608
			path = path.replace("\\", "\\\\"); //$NON-NLS-1$ //$NON-NLS-2$
633
		rw.voidEval("tmpmat <- as.matrix(read.table(file=\"" + path + "\", fileEncoding=\"UTF-8\"));"); //$NON-NLS-1$ //$NON-NLS-2$
634

  
635
		rw.voidEval("a <- colnames(tmpmat)"); //$NON-NLS-1$
636
		rw.voidEval("if (length(grep(\"X.+\", a)) == length(a)) {"+ //$NON-NLS-1$
637
				" strcut <- function(x) substr(x, 2, nchar(x)) ;"+ //$NON-NLS-1$
638
				" for(i in 1:length(a)) { a[i] <- strcut(a[i]) } ; "+ //$NON-NLS-1$
639
				" colnames(tmpmat) <- a; }"); //$NON-NLS-1$
640

  
641
		// check number of columns
642
		int colsize = rw.eval("dim(tmpmat)[2]").asInteger(); //$NON-NLS-1$
643
		int linesize = rw.eval("dim(tmpmat)[1]").asInteger(); //$NON-NLS-1$
644
		int ncol = this.getNColumns();
645
		if (colsize != ncol) {
646
			Log.severe(TXMCoreMessages.bind(StatsEngineCoreMessages.matrixImplColonErrorColonColumnSizeDiffersColonOriginal, ncol, colsize));
647
			return false;
648 609
		}
610
		
611
/*
612
t2 <- as.matrix(read.table(file = "/home/mdecorde/TEMP/export (copie).csv", row.names=1, skip=1));
613
header <-scan(file = "/home/mdecorde/TEMP/export (copie).csv", nlines=1, sep="\t", fileEncoding="UTF-8", what=character())
614
header <- header[2:length(header)];
615
t3 <- t2[1:nrow(t2),];
616
colnames(t3) <- header;
617
 */
618
		rw.voidEval("tmpmat <- as.matrix(read.table(file=\"" + path + "\", row.names=1, skip=1, fileEncoding=\"UTF-8\"));"); //$NON-NLS-1$ //$NON-NLS-2$
619
		rw.voidEval("tmpheader <- scan(file=\"" + path + "\", what = character(), nlines=1, sep=\"\t\", fileEncoding=\"UTF-8\");"); //$NON-NLS-1$ //$NON-NLS-2$
620
		rw.voidEval("tmpheader <- tmpheader[2:length(tmpheader)];"); //$NON-NLS-1$
621
		rw.voidEval("tmpmat <- tmpmat[1:nrow(tmpmat),];"); //$NON-NLS-1$
622
		rw.voidEval("colnames(tmpmat) <- tmpheader"); //$NON-NLS-1$
649 623

  
650 624
		rw.eval(symbol +"<- tmpmat"); //$NON-NLS-1$
651 625

  
652
		nline--;
653
		if (nline != linesize) {
654
			System.out.println("Warning: some lines have not been read. Check your file format (header, encoding=UTF-8, separator=TAB).");
655
			System.out.println(TXMCoreMessages.bind("Number of lines in the file={0}.", nline));
656
			System.out.println(TXMCoreMessages.bind("Number of lines in the matrix={0}.", linesize));
657
		}
658 626
		return true;
659 627
	}
660 628

  
......
1047 1015
			String path = f.getAbsolutePath().replace("\\", "\\\\"); //$NON-NLS-1$ //$NON-NLS-2$
1048 1016
			org.txm.statsengine.r.core.RWorkspace.getRWorkspaceInstance().safeEval(
1049 1017
					"write.table(" + symbol + ", file=\"" + path + "\""//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1050
					+ ", fileEncoding=\""+encoding+"\"" //$NON-NLS-1$ //$NON-NLS-2$
1018
					+ ", col.names = NA, row.names = TRUE, fileEncoding=\""+encoding+"\"" //$NON-NLS-1$ //$NON-NLS-2$
1051 1019
					+ ", sep=\""+colseparator+"\");"); //$NON-NLS-1$ //$NON-NLS-2$
1052 1020
		} catch (Exception e1) {
1053 1021
			org.txm.utils.logger.Log.printStackTrace(e1);

Formats disponibles : Unified diff