001: /**
002: * @author Olivier BEDEL
003: * Laboratoire RESO UMR 6590 CNRS
004: * Bassin Versant du Jaudy-Guindy-Bizien
005: * 26 oct. 2004
006: *
007: */package org.openjump.sigle.plugin.joinTable;
008:
009: import java.util.ArrayList;
010: import java.util.List;
011:
012: import javax.swing.JComboBox;
013: import javax.swing.JFileChooser;
014: import javax.swing.JPanel;
015: import javax.swing.filechooser.FileFilter;
016:
017: import com.vividsolutions.jump.I18N;
018: import com.vividsolutions.jump.feature.FeatureSchema;
019: import com.vividsolutions.jump.task.TaskMonitor;
020: import com.vividsolutions.jump.workbench.WorkbenchContext;
021: import com.vividsolutions.jump.workbench.model.Layer;
022: import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn;
023: import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory;
024: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
025: import com.vividsolutions.jump.workbench.plugin.ThreadedBasePlugIn;
026: import com.vividsolutions.jump.workbench.ui.ErrorDialog;
027: import com.vividsolutions.jump.workbench.ui.GUIUtil;
028: import com.vividsolutions.jump.workbench.ui.GenericNames;
029: import com.vividsolutions.jump.workbench.ui.MenuNames;
030: import com.vividsolutions.jump.workbench.ui.MultiInputDialog;
031: import com.vividsolutions.jump.workbench.ui.WorkbenchFrame;
032: import com.vividsolutions.jump.workbench.ui.plugin.FeatureInstaller;
033: import com.vividsolutions.jump.workbench.ui.plugin.SaveProjectAsPlugIn;
034:
035: /**
036: * @author Olivier BEDEL
037: * Laboratoire RESO UMR 6590 CNRS
038: * Bassin Versant du Jaudy-Guindy-Bizien
039: * 26 oct. 2004
040: * @license Licence CeCILL http://www.cecill.info/
041: *
042: * [sstein 25.March.2007] modified for using it in normal menu and not layerpopupmenu
043: *
044: */
045: public class JoinTablePlugIn extends ThreadedBasePlugIn {
046: protected static String name = "JoinTable";
047: public static FileFilter JOIN_TABLE_FILE_FILTER = null;
048: private Layer layer;
049: private JFileChooser fileChooser;
050: private MultiInputDialog dialog;
051: private MultiInputDialog layerdialog;
052: private String LAYER_ATTRIBUTES = null;
053: private String TABLE_ATTRIBUTES = null;
054: private String sLAYER = GenericNames.LAYER;
055:
056: public void initialize(PlugInContext context) throws Exception {
057:
058: // initialisation du filtre de fichier
059: JoinTablePlugIn.JOIN_TABLE_FILE_FILTER = GUIUtil
060: .createFileFilter(
061: I18N
062: .get("org.openjump.sigle.plugin.joinTable.text_file"),
063: new String[] { "txt", "text" });
064: LAYER_ATTRIBUTES = I18N
065: .get("org.openjump.sigle.plugin.joinTable.layer_field");
066: TABLE_ATTRIBUTES = I18N
067: .get("org.openjump.sigle.plugin.joinTable.table_field");
068:
069: /*
070: context.getFeatureInstaller().addPopupMenuItem(
071: context.getWorkbenchContext().getWorkbench().getFrame().getLayerNamePopupMenu(),
072: this,
073: I18N.get("org.openjump.sigle.plugin.joinTable.Join_data")+"{pos:14}" ,
074: false,
075: null,
076: new EnableCheckFactory(context.getWorkbenchContext()).createAtLeastNLayersMustExistCheck(1));
077: */
078:
079: FeatureInstaller featureInstaller = new FeatureInstaller(
080: context.getWorkbenchContext());
081: featureInstaller.addMainMenuItem(this , //exe
082: new String[] { MenuNames.TOOLS,
083: MenuNames.TOOLS_EDIT_ATTRIBUTES }, //menu path
084: this .getName(), //name methode .getName recieved by AbstractPlugIn
085: false, //checkbox
086: null, //icon
087: new EnableCheckFactory(context.getWorkbenchContext())
088: .createAtLeastNLayersMustExistCheck(1)); //enable check
089:
090: //Joindre des données -> I18N.get(name,"org.OpenJUMP.layer.joinTable.JoinTablePlugin.MenuName")
091: fileChooser = GUIUtil.createJFileChooserWithExistenceChecking();
092: fileChooser
093: .setDialogTitle(I18N
094: .get("org.openjump.sigle.plugin.joinTable.Choose_file_data_to_join"));
095: fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
096: fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
097: fileChooser.setMultiSelectionEnabled(false);
098: GUIUtil.removeChoosableFileFilters(fileChooser);
099: fileChooser.addChoosableFileFilter(JOIN_TABLE_FILE_FILTER);
100: fileChooser.addChoosableFileFilter(GUIUtil.ALL_FILES_FILTER);
101: fileChooser.setFileFilter(JOIN_TABLE_FILE_FILTER);
102: }
103:
104: public String getName() {
105: return I18N
106: .get("org.openjump.sigle.plugin.joinTable.Join_data");
107: }
108:
109: public boolean execute(PlugInContext context) throws Exception {
110: reportNothingToUndoYet(context);
111:
112: if (JFileChooser.APPROVE_OPTION != fileChooser
113: .showOpenDialog(context.getWorkbenchFrame())) {
114: return false;
115: }
116:
117: //-- get Layer -------------------------------------
118: // load string again for correct language
119: sLAYER = GenericNames.LAYER;
120: //-- Unlike ValidatePlugIn, here we always call #initDialog because we want
121: // to update the layer comboboxes. [Jon Aquino]
122: initLayerDialog(context);
123: layerdialog.setVisible(true);
124:
125: if (!layerdialog.wasOKPressed()) {
126: return false;
127: } else {
128: layer = layerdialog.getLayer(sLAYER);
129: }
130: //-- check if attributes exist
131: if (layer.getFeatureCollectionWrapper().getFeatureSchema()
132: .getAttributeCount() == 0) {
133: ErrorDialog
134: .show(
135: context.getWorkbenchFrame(),
136: I18N
137: .get("org.openjump.sigle.plugin.joinTable.Unable_to_join_data"),
138: I18N
139: .get("org.openjump.sigle.plugin.joinTable.Layer_has_no_field"),
140: "");
141: return false;
142: }
143:
144: JoinTable jt = new JoinTable(fileChooser.getSelectedFile()
145: .getAbsolutePath());
146:
147: //-- get unique field/attribute --------------------
148: initDialog(context, jt, layer);
149: dialog.setVisible(true);
150: if (!dialog.wasOKPressed()) {
151: jt.dispose();
152: return false;
153: }
154: //System.out.println("indice du champ de la table : " + dialog.getComboBox(TABLE_ATTRIBUTES).getSelectedIndex() );
155: //System.out.println("indice du champ de la couche : " + dialog.getComboBox(LAYER_ATTRIBUTES).getSelectedIndex() );
156: jt.setKeyIndex(dialog.getComboBox(TABLE_ATTRIBUTES)
157: .getSelectedIndex());
158: jt.build();
159: //jointure sur la couche en memoire
160: jt.join(layer, dialog.getComboBox(LAYER_ATTRIBUTES)
161: .getSelectedIndex());
162:
163: //liberation memoire
164: jt.dispose();
165: System.gc();
166: return true;
167: }
168:
169: public void run(TaskMonitor monitor, PlugInContext context)
170: throws Exception {
171: //left emtpy
172: }
173:
174: private void initDialog(PlugInContext context, JoinTable jt,
175: Layer layer) {
176:
177: FeatureSchema schema = layer.getFeatureCollectionWrapper()
178: .getFeatureSchema();
179:
180: ArrayList layerAttributes = new ArrayList(schema
181: .getAttributeCount());
182:
183: for (int i = 0; i < schema.getAttributeCount(); i++)
184: layerAttributes.add(i, schema.getAttributeName(i));
185:
186: dialog = new MultiInputDialog(
187: context.getWorkbenchFrame(),
188: I18N
189: .get("org.openjump.sigle.plugin.joinTable.Matching_fields"),
190: true);
191:
192: dialog
193: .setSideBarDescription(I18N
194: .get("org.openjump.sigle.plugin.joinTable.Choose_fields_to_join"));
195:
196: dialog.addComboBox(LAYER_ATTRIBUTES, layerAttributes.get(0),
197: layerAttributes, null);
198: dialog.addComboBox(TABLE_ATTRIBUTES, jt.getFieldName(0), jt
199: .getFieldNames(), null);
200: GUIUtil.centreOnWindow(dialog);
201: }
202:
203: private void initLayerDialog(PlugInContext context) {
204: layerdialog = new MultiInputDialog(context.getWorkbenchFrame(),
205: "", true);
206:
207: //dialog.setSideBarImage(IconLoader.icon("Overlay.gif"));
208: layerdialog.setSideBarDescription(I18N
209: .get("org.openjump.sigle.plugin.joinTable.MenuName"));
210: String fieldName = sLAYER;
211: JComboBox addLayerComboBox = layerdialog.addLayerComboBox(
212: fieldName, context.getCandidateLayer(0), null, context
213: .getLayerManager());
214: GUIUtil.centreOnWindow(layerdialog);
215: }
216: }
|