summaryrefslogtreecommitdiffstats
path: root/logo/src/xlogo/gui/translation/MyTable.java
blob: cd8c410bef31f18add8785f503dfbc9d15f56a87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* XLogo4Schools - A Logo Interpreter specialized for use in schools, based on XLogo by Loic Le Coq
 * Copyright (C) 2013 Marko Zivkovic
 * 
 * Contact Information: marko88zivkovic at gmail dot com
 * 
 * This program is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the Free 
 * Software Foundation; either version 2 of the License, or (at your option) 
 * any later version.  This program is distributed in the hope that it will be 
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
 * Public License for more details.  You should have received a copy of the 
 * GNU General Public License along with this program; if not, write to the Free 
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA 02110-1301, USA.
 * 
 * 
 * This Java source code belongs to XLogo4Schools, written by Marko Zivkovic
 * during his Bachelor thesis at the computer science department of ETH Zurich,
 * in the year 2013 and/or during future work.
 * 
 * It is a reengineered version of XLogo written by Loic Le Coq, published
 * under the GPL License at http://xlogo.tuxfamily.org/
 * 
 * Contents of this file were initially written by Loic Le Coq,
 * modifications, extensions, refactorings migh have been applied by Marko Zivkovic 
 */

package xlogo.gui.translation;
import java.util.Locale;
import java.util.ResourceBundle;
import java.awt.Component;
import java.util.Vector;
import java.util.Enumeration;
import java.util.Collections;

import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.AbstractCellEditor;
import javax.swing.table.TableCellEditor;
import javax.swing.JTextArea;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.event.*;
import javax.swing.JScrollPane;

import java.awt.event.*;

import xlogo.gui.Searchable;
import xlogo.storage.workspace.Language;
import xlogo.Logo;
public class MyTable extends JPanel implements Searchable{
	private static final long serialVersionUID = 1L;
	private JTable table;
	private JScrollPane scrollPane;
	private TranslateXLogo tx;
	private String id;
	private String action;
	private String bundle;
	private Vector<String>  keys;
	protected MyTable(TranslateXLogo tx, String action, String id,String bundle){
		this.tx=tx;
		this.action=action;
		this.id=id;
		this.bundle=bundle;
		initGui();
	}

    public void setColumnSize(){   
            for (int i = 0 ; i < table.getColumnCount() ; i++)
        {
           table.getColumnModel().getColumn(i).setPreferredWidth(200);
        }
    }
    protected String getValue(int a, int b){
    	return table.getValueAt(a, b).toString();
    }
	private void initGui(){
		setLayout(new java.awt.BorderLayout());
		table=new JTable(new MyModel(bundle,action,id));

		MultiLineCellEditor editor = new MultiLineCellEditor(table);
		table.setDefaultEditor(String.class,editor);

		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		scrollPane = new JScrollPane(table);
		//table.setFillsViewportHeight(true);
		// Only one selection is possible
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		// You can only select Cell
		table.setCellSelectionEnabled (true);
		setColumnSize();
		add(scrollPane,java.awt.BorderLayout.CENTER);
		
	}
	 protected Vector<String> getKeys(){
		 return keys;
	 }
	class MultiLineCellEditor extends AbstractCellEditor implements TableCellEditor {
		private static final long serialVersionUID = 1L;
	  MyTextArea textArea;
		JTable table;
	 
		public MultiLineCellEditor(JTable ta) {
			super();
			table = ta;
			// this component relies on having this renderer for the String class
			MultiLineCellRenderer renderer = new MultiLineCellRenderer();
			table.setDefaultRenderer(String.class,renderer);
//	                JOptionPane.showMessageDialog(null,"jui ds l'editor","h",JOptionPane.INFORMATION_MESSAGE);
			textArea = new MyTextArea();
			textArea.setLineWrap(true);
			textArea.setWrapStyleWord(true);
			for(int i=0;i<table.getRowCount();i++) updateRow(i);
		}
	 
		/** This method determines the height in pixel of a cell given the text it contains */
		private int cellHeight(int row,int col) {
			if (row == table.getEditingRow() && col == table.getEditingColumn())
			  return textArea.getPreferredSize().height;
		  else
			  return table.getDefaultRenderer(String.class).getTableCellRendererComponent(table,
				  table.getModel().getValueAt(row,col),false,false,row,col).getPreferredSize().height;
		}
	 
		void cellGrewEvent(int row,int column) {
			updateRow(row);
		}
	 
		void cellShrankEvent(int row,int column) {
			updateRow(row);
		}
	 
		void updateRow(int row) {
			int maxHeight = 0;
			for(int j=0;j<table.getColumnCount();j++) {
				int ch;
				if ((ch = cellHeight(row,j)) > maxHeight) {
				  maxHeight = ch;
				}
			}
			table.setRowHeight(row,maxHeight);
		}
	 
		public Object getCellEditorValue() {
			return textArea.getText();
	  }	 
		public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,
							int row, int column) {
			textArea.setText(table.getValueAt(row,column).toString());
			textArea.rowEditing = row;
			textArea.columnEditing = column;
			textArea.lastPreferredHeight = textArea.getPreferredSize().height;
		
			return textArea;

		}
	 
		class MyTextArea extends JTextArea implements KeyListener {
			private static final long serialVersionUID = 1L;
			int lastPreferredHeight = 0;
			int rowEditing;
			int columnEditing;
	 
			MyTextArea() {
				addKeyListener(this);
				// This is a fix to Bug Id 4256006
				addAncestorListener( new AncestorListener(){
					public void ancestorAdded(AncestorEvent e){
					  requestFocus();
				  }
				  public void ancestorMoved(AncestorEvent e){}
				  public void ancestorRemoved(AncestorEvent e){}
				});
			}
	 
			public void keyPressed(KeyEvent e) {}
			public void keyReleased(KeyEvent e) {}
			public void keyTyped(KeyEvent e) {
				if (getPreferredSize().getHeight() > lastPreferredHeight) {
					lastPreferredHeight = getPreferredSize().height;
					cellGrewEvent(rowEditing,columnEditing);
					// this will trigger the addition of extra lines upon the cell growing and
					// prevent all the text being lost when the cell grows to the point of requiring
					// scrollbars
					table.setValueAt(getText(),rowEditing,columnEditing);
				}
				else if (getPreferredSize().getHeight() < lastPreferredHeight) {
					lastPreferredHeight = getPreferredSize().height;
					cellShrankEvent(rowEditing,columnEditing);
				}
				else if (table.getValueAt(rowEditing,columnEditing).equals(""))
					table.setValueAt(getText(),rowEditing,columnEditing);
	 
		  }
		}
	}
	
	class MyModel extends AbstractTableModel {
		private static final long serialVersionUID = 1L;
		private String[] columnNames;
		private String[][] rowData;
		String action;
		String id;
		
		MyModel(String bundle,String action, String id){
			this.action=action;
			this.id=id;
			
			if (action.equals(TranslateXLogo.CREATE)){
				// Initilaize all Column Names
				String[] tmp=Logo.translationLanguage;
				columnNames=new String[tmp.length+1];
				columnNames[0]=id;
				for (int i=1;i<columnNames.length;i++){
					columnNames[i]=tmp[i-1];
				}
			}
			else columnNames=Logo.translationLanguage;
			buildRowData(bundle, action,id);
		}
		public String getColumnName(int col) {
	        return columnNames[col].toString();
	    }
	    public int getRowCount() { return rowData.length; }
	    public int getColumnCount() { return columnNames.length; }
	    public Object getValueAt(int row, int col) {
	        return rowData[row][col];
	    }
	    public boolean isCellEditable(int row, int col)
	        { 
	    	if (action.equals(TranslateXLogo.CONSULT)) return false;
	    	else if (action.equals(TranslateXLogo.CREATE)) {
	    		if (col==0) return true;
	    		else return false;
	    	}
	    	else if (action.equals(TranslateXLogo.MODIFY)) {
	    		if (col==Integer.parseInt(id)) return true;
	    		else return false;
	    	}
	    	else if (action.equals(TranslateXLogo.COMPLETE)) {
	     		if (col==Integer.parseInt(id)) {
	     			
	     			if (rowData[row][col].equals("")) return true;
	     			else return false;
	     		}
	    		else return false;
	    	}
	    	return true; }
	    public void setValueAt(Object value, int row, int col) {
	        rowData[row][col] = value.toString();
	        fireTableCellUpdated(row, col);
	    }
	    public Class<? extends Object> getColumnClass(int c) {
	        return getValueAt(0, c).getClass();

	    }

	    private void buildRowData(String bundle,String action, String id){
	    	ResourceBundle[] rb=new ResourceBundle[getColumnCount()];
	    	// initialize all ResourceBundle
	    	for(int i=0;i<getColumnCount();i++){
	       		Locale locale = Language.getLanguage(i).getLocale();
	       		// In CREATE Mode, when i=getColumnCount(), the last locale is null
	       		if (null==locale) break;
	       		rb[i] = ResourceBundle.getBundle(bundle, locale);
	    	}
	    	keys=new Vector<String>();
	    	Enumeration<String> en=rb[0].getKeys();
	    	while(en.hasMoreElements()){
	    		String value=en.nextElement();
	    		keys.add(value);
	    	}
	    	Collections.sort(keys);
	    	rowData=new String[keys.size()][getColumnCount()];
	    	int row=0;
	    	for (int j=0;j<keys.size();j++) {
	       		String key=keys.get(j).toString();
	       		
	       		for(int i=0;i<getColumnCount();i++){
	       			if (action.equals(TranslateXLogo.CREATE)){
	       				if (i==0) rowData[row][0]="";
	       				else rowData[row][i]=rb[i-1].getString(key);
	       			}
	       			else {
	    	     			String element=rb[i].getString(key);
	    	     			if (element.equals("")) {
	    	     				rowData[row][i]="";
	    	     			}
	    	     			else rowData[row][i]=element;
	       			}
		    	}	
	    		row++;
	    	}
	    }
	}	
	class MultiLineCellRenderer extends JTextArea implements TableCellRenderer {
		private static final long serialVersionUID = 1L;
	  public MultiLineCellRenderer() {
	    setEditable(false);
	    setLineWrap(true);
	    setWrapStyleWord(true);
	  }
	 
	  public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected, boolean hasFocus, int row, int column) {
		  
	    if (value instanceof String) {
	    	if (value.toString().equals("")) setBackground(new java.awt.Color(255,200,200));
	    	else setBackground(java.awt.Color.WHITE);
	    	setText((String)value);
	    	if (isSelected) setBackground(java.awt.Color.LIGHT_GRAY);
	      // set the table's row height, if necessary
				//updateRowHeight(row,getPreferredSize().height);
	    }
	    else
	      setText("");
	    return this;
	  }
	}
	int selectedRow=0;
	int selectedColumn=0;
	 public boolean find(String element,boolean forward){
		 int row=table.getSelectedRow();
		 int col=table.getSelectedColumn();
		 if (row!=-1) {
			 if (row==table.getRowCount()-1) {
				 selectedRow=0;
				 if (col==table.getColumnCount()-1) selectedColumn=0;
				 else selectedColumn=col+1;
			 }
			 else selectedRow=row+1;
		 } 
		 int start=selectedRow;
		 for (int c=selectedColumn;c<table.getColumnCount();c++){
			 	for(int r=start;r<table.getRowCount();r++){
				 String value=table.getValueAt(r, c).toString();
				 int index=value.indexOf(element);
				 if (index!=-1) {
					 table.clearSelection();
					 if (r==table.getRowCount()-1){
						 selectedRow=0;
						 if (c==table.getColumnCount()-1) selectedColumn=0;
						 else selectedColumn=c+1;
					 }
					 else {
						 selectedRow=r+1;
					 	 selectedColumn=c;
					 }
					 boolean b=table.editCellAt(r, c);
					 if (!b) {
						 table.changeSelection(r, c,false,false);
					 }
//					 System.out.println(selectedRow+" "+selectedColumn);
					 return true;
				 }
			 }
			 start=0;
		 }
		 selectedColumn=0;
		 selectedRow=0;
		 table.changeSelection(0, 0,false,false);
		 return false;
	 }
	public void replace(String element, boolean forward){
		
	}
	public void replaceAll(String element, String substitute){
		
	}
	public void removeHighlight(){
		tx.resetSearchFrame();
	}
	
}