package vciw.preferences; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Enumeration; import java.util.LinkedHashSet; import java.util.List; import java.util.Properties; import java.util.Set; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.Path; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jface.dialogs.IInputValidator; import org.eclipse.jface.dialogs.InputDialog; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.preference.DirectoryFieldEditor; import org.eclipse.jface.preference.FieldEditorPreferencePage; import org.eclipse.jface.preference.StringFieldEditor; import org.eclipse.swt.SWT; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableColumn; import org.eclipse.swt.widgets.TableItem; import org.eclipse.swt.widgets.Text; import org.eclipse.ui.IWorkbench; import org.eclipse.ui.IWorkbenchPreferencePage; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.EntityResolver; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import vciw.Activator; /** * This class represents a preference page that is contributed to the * Preferences dialog. By subclassing FieldEditorPreferencePage, we * can use the field support built into JFace that allows us to create a page * that is small and knows how to save, restore and apply itself. *
* This page is used to modify preferences only. They are stored in the * preference store that belongs to the main plug-in class. That way, * preferences can be accessed directly via the preference store. */ public class SamplePreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage { private DirectoryFieldEditor VCI_Home; private DirectoryFieldEditor Corba_Home; private DirectoryFieldEditor javaHome; private static String VCI_Home_Path; private static String VCI_Corba_Path; private static String url = ""; private static String username = ""; private static String password = ""; private static String corboInfo = ""; private String[] columnTitles = { "index", "name", "class", "on" }; private SortedProperties properties = new SortedProperties(); Table table = null; private boolean initTable = false; private TableItem selectedRow = null; // private boolean reverseSort = false; // private String lastSelection = null; public SamplePreferencePage() { super(GRID); setPreferenceStore(Activator.getDefault().getPreferenceStore()); setDescription(" DataSource and Corba Setting of VCI PlatFrom "); } /** * Creates the field editors. Field editors are abstractions of the common * GUI blocks needed to manipulate various types of preferences. Each field * editor knows how to save and restore itself. */ public void createFieldEditors() { Composite composite = new Composite(getFieldEditorParent(), SWT.NULL); GridLayout layout = new GridLayout(); composite.setLayout(layout); GridData data = new GridData(GridData.FILL_HORIZONTAL); data.grabExcessHorizontalSpace = true; composite.setLayoutData(data); Group groupmain = new Group(composite, 1); groupmain.setText("VCI Home ÉèÖÃ"); GridData gd = new GridData(GridData.FILL_BOTH); groupmain.setLayoutData(gd); VCI_Home = new DirectoryFieldEditor(PreferenceConstants.P_PATH_VCIHOME, "VCI_Home:", groupmain); addSeparator(composite); addFirstSection(composite); //addSeparator(composite); //addSecondSection(composite); addSeparator(composite); addCorbaServerInfoSection(composite); } class CorbaInput extends InputDialog { CorbaInput(Shell paramShell, String paramString1, String paramString2, String paramString3, IInputValidator paramIInputValidator) { super(paramShell, paramString1, paramString2, paramString3, paramIInputValidator); } private String clazzName = ""; protected void okPressed() { clazzName = classtext.getText(); super.okPressed(); } private Text classtext; protected Control createDialogArea(Composite parent) { Composite composite = (Composite) super.createDialogArea(parent); this.classtext = new Text(composite, getInputTextStyle()); this.classtext.setLayoutData(new GridData(768)); this.classtext.setText("class"); this.classtext.addModifyListener(new ModifyListener() { public void modifyText(ModifyEvent e) { validateInput(); } }); return classtext; } public String getClassText() { return clazzName; } } private void addCorbaServerInfoSection(Composite composite) { /* GridLayout layout = new GridLayout(); composite.setLayout(layout); GridData data = new GridData(GridData.FILL_HORIZONTAL); data.grabExcessHorizontalSpace = true; composite.setLayoutData(data); Group groupmain = new Group(composite, 1); groupmain.setText("VCI Home ÉèÖÃ"); GridData gd = new GridData(GridData.FILL_BOTH); groupmain.setLayoutData(gd); VCI_Home = new DirectoryFieldEditor(PreferenceConstants.P_PATH_VCIHOME, "VCI_Home:", groupmain); */ Group group = new Group(composite, 1);//32); group.setText("Corba ·þÎñÅäÖÃ"); GridData data = new GridData(GridData.FILL_BOTH);//4, 16777216, true, false); group.setLayoutData(data); GridLayout layout = new GridLayout(); layout.numColumns = 3; group.setLayout(layout); // table table = new Table(group, 68356); GridData gridData = new GridData(GridData.FILL_BOTH); gridData.heightHint = convertVerticalDLUsToPixels(150); table.setLayoutData(gridData); table.setHeaderVisible(true); table.setLinesVisible(true); table.setFont(group.getFont()); table.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { if (e.item != null) selectedRow = (TableItem) e.item; return; } }); Button buttoninit = new Button(group, SWT.PUSH); buttoninit.setText("ÐÂÔö"); buttoninit.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { if (properties.get("servicecount") == null) { MessageDialog.openInformation(getShell(), "Ìáʾ", "Çë³õʼ»¯"); return; } IInputValidator validator = new IInputValidator() { public String isValid(String newText) { return ((newText.length() == 0) ? "" : null); } }; CorbaInput dialog = new CorbaInput(getShell(), "corba service setting", "", "name", validator); int result = dialog.open(); if (result != 0) { return; } String name = dialog.getValue(); String clazz = dialog.getClassText(); int newrow = Integer.parseInt((String) properties.get("servicecount")); String index = Integer.toString(newrow); TableItem item = new TableItem(table, newrow); item.setText(new String[] { index, name, clazz, "Y" }); // save // properties.put("service" + newrow + ".name", name); // properties.put("service" + newrow + ".class", clazz); // / File file = new File(getPreferenceStore().getString(PreferenceConstants.P_PATH_VCIHOME) + "\\src\\properties\\conf.properties"); InputStream inStream = null; OutputStream out = null; try { inStream = new FileInputStream(file); properties.load(inStream); if (inStream != null) { inStream.close(); } out = new FileOutputStream(file); properties.setProperty("service" + newrow + ".name", name); properties.setProperty("service" + newrow + ".class", clazz); properties.setProperty("servicecount", newrow + 1 + ""); properties.store(out, "conf.properties version by Eclipse"); if (out != null) { out.close(); } } catch (IOException ecx) { MessageDialog.openInformation(getShell(), "Ìáʾ", ecx.getMessage()); } } }); Button buttondel = new Button(group, SWT.PUSH); buttondel.setText("ɾ³ý"); buttondel.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { if (selectedRow != null) { boolean istrue = MessageDialog.openConfirm(getShell(), "Ìáʾ", "ÊÇ·ñɾ³ý"); if (istrue) { int row = 0; // table.remove(table.get); TableItem[] items = table.getItems(); for (int i = 0; i < items.length; i++) { if (items[i].getText().equals(selectedRow.getText())) { row = i; break; } } table.remove(row); File file = new File(getPreferenceStore().getString(PreferenceConstants.P_PATH_VCIHOME) + "\\src\\properties\\conf.properties"); InputStream inStream = null; OutputStream out = null; try { inStream = new FileInputStream(file); properties.load(inStream); if (inStream != null) { inStream.close(); } out = new FileOutputStream(file); // properties.remove("service" + (row) + ".name"); // properties.remove("service" + (row) + ".class"); properties.setProperty("service" + row + ".class", ""); properties.setProperty("service" + row + ".name", ""); properties.setProperty("servicecount", Integer.parseInt((String) properties.get("servicecount")) - 1 + ""); properties.store(out, "conf.properties version by Eclipse"); if (out != null) { out.close(); } } catch (IOException ecx) { MessageDialog.openInformation(getShell(), "Ìáʾ", ecx.getMessage()); } } else { return; } } } }); int[] columnWidths = { convertHorizontalDLUsToPixels(35), convertHorizontalDLUsToPixels(100), convertHorizontalDLUsToPixels(140), convertHorizontalDLUsToPixels(35), convertHorizontalDLUsToPixels(130) }; for (int i = 0; i < columnTitles.length; ++i) { TableColumn tableColumn = new TableColumn(table, 0); tableColumn.setWidth(columnWidths[i]); tableColumn.setText(this.columnTitles[i]); final int columnIndex = i; tableColumn.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { sort(columnIndex); } }); } } @SuppressWarnings({ "unchecked", "rawtypes" }) private void sort(int column) { TableItem[] items = table.getItems(); Arrays.sort(items, new Comparator() { public int compare(Object a, Object b) { TableItem info1 = (TableItem) a; TableItem info2 = (TableItem) b; return info2.getText(0).compareTo(info1.getText(0)); } }); System.out.println(); // table.removeAll(); // for (int i = 0; i < sorted.length; i++) { // TableItem item = new TableItem(table, i); // item.setText(sorted[i].getText()); // } // for (int i = 0; i < items.length; ++i) { // items[i].setText(createRow(this.bundleGroupInfos[i])); // items[i].setData(this.bundleGroupInfos[i]); // } } private void addFirstSection(Composite parent) { //Composite composite = createDefaultComposite(parent); Group groupDB = new Group(parent, 1); groupDB.setText("²ÎÊýÉèÖÃ"); GridData gd = new GridData(GridData.FILL_BOTH); groupDB.setLayoutData(gd); final StringFieldEditor dbIP = new StringFieldEditor(PreferenceConstants.P_STRING_URL_IP, "Êý¾Ý¿âµØÖ· (&A):", groupDB); addField(dbIP); //addField(new StringFieldEditor(PreferenceConstants.P_STRING_URL_IP, "Êý¾Ý¿âµØÖ· &ip:", group)); final StringFieldEditor dbInstance = new StringFieldEditor(PreferenceConstants.P_STRING_URL_INSTANCE, "Êý¾Ý¿âʵÀý (&D):", groupDB); addField(dbInstance); final StringFieldEditor dbUser = new StringFieldEditor(PreferenceConstants.P_STRING_USER, "Óû§ (&U):", groupDB); addField(dbUser); final StringFieldEditor dbPws = new StringFieldEditor(PreferenceConstants.P_STRING_PWD, "ÃÜÂë (&P):", groupDB); addField(dbPws); javaHome = new DirectoryFieldEditor(PreferenceConstants.java_home, "Java home:", groupDB); addField(javaHome); addField(VCI_Home); Group groupCorba = new Group(parent, 1); groupCorba.setText("Corba ÉèÖÃ"); GridData gdCorba = new GridData(GridData.FILL_BOTH); groupCorba.setLayoutData(gdCorba); final StringFieldEditor dbCorbaIP = new StringFieldEditor(PreferenceConstants.P_PATH_COMBOIP, "Corba µØÖ·(&I):", groupCorba); addField(dbCorbaIP); final StringFieldEditor corbaCommand = new StringFieldEditor(PreferenceConstants.combo_command, "Corba ÃüÁî(&C):", groupCorba); addField(corbaCommand); Corba_Home = new DirectoryFieldEditor(PreferenceConstants.P_PATH_COMBOHOME, "Corba_&Home:", groupCorba); addField(Corba_Home); // Group groupOp = new Group(parent, 1); // groupOp.setText("²Ù×÷"); // GridData opCorba = new GridData(GridData.FILL_BOTH); // groupCorba.setLayoutData(opCorba); Group groupOP = new Group(parent, 1);//32); groupOP.setText("²Ù×÷"); GridData dataOP = new GridData(GridData.FILL_BOTH);//4, 16777216, true, false); groupOP.setLayoutData(dataOP); GridLayout layout = new GridLayout(); layout.numColumns = 3; groupOP.setLayout(layout); Button buttoninit = new Button(groupOP, SWT.PUSH); buttoninit.setText("³õʼ»¯..."); buttoninit.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { int returnint = resetEclipseHomeVariable(); if (returnint == 1) { return; } String psdefault_vcihome = VCI_Home.getStringValue(); if (psdefault_vcihome != null && !psdefault_vcihome.equals("")) { // ²éѯxml getXMlInfo(psdefault_vcihome); getPreferenceStore().setValue(PreferenceConstants.P_PATH_VCIHOME, psdefault_vcihome); if (Corba_Home.getStringValue().equals("")) { Corba_Home.setStringValue(psdefault_vcihome + "\\JacORB"); getPreferenceStore().setValue(PreferenceConstants.P_PATH_COMBOHOME, psdefault_vcihome + "\\JacORB"); } if (getPreferenceStore().getString(PreferenceConstants.combo_command).equals("")) { getPreferenceStore().setValue(PreferenceConstants.combo_command, "ns -p 30000"); } if (javaHome.getStringValue().equals("")) { javaHome.setStringValue(psdefault_vcihome + "\\jre"); getPreferenceStore().setValue(PreferenceConstants.java_home, psdefault_vcihome + "\\jre"); } getPreferenceStore().setValue(PreferenceConstants.P_STRING_URL_IP, getIP()); getPreferenceStore().setValue(PreferenceConstants.P_STRING_URL_INSTANCE, getInstance()); getPreferenceStore().setValue(PreferenceConstants.P_STRING_USER, username); getPreferenceStore().setValue(PreferenceConstants.P_STRING_PWD, password); corboInfo = getCombaInfo(psdefault_vcihome); // ³õʼ»¯ initCorbaServer(); getPreferenceStore().setValue(PreferenceConstants.P_PATH_COMBOIP, getCombaip()); } initialize(); MessageDialog.openInformation(getShell(), "Ìáʾ", "³õʼ»¯Íê³É" + "\n"); } }); Button button = new Button(groupOP, SWT.PUSH); button.setText("²âÊÔÊý¾Ý¿â ..."); button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { try { testDBConnect(); // ±£´æÊý¾Ý¿âÐÅÏ¢ saveXML(); // ±£´æCombaÐÅÏ¢ setCombaInfo(); } catch (SQLException e1) { MessageDialog.openInformation(getShell(), "Ìáʾ", "Êý¾Ý¿âÁ¬½Óʧ°Ü" + "\n" + e1.getMessage()); } } }); Button buttonComba = new Button(groupOP, SWT.PUSH); buttonComba.setText("Æô¶¯Corba·þÎñ..."); buttonComba.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { if (Corba_Home.getStringValue().equals("")) { MessageDialog.openInformation(getShell(), "Ìáʾ", "ÇëÑ¡ÔñVCI_Corba·¾¶"); return; } getPreferenceStore().setValue(PreferenceConstants.P_PATH_COMBOHOME, Corba_Home.getStringValue()); getPreferenceStore().setValue(PreferenceConstants.java_home, javaHome.getStringValue()); getPreferenceStore().setValue(PreferenceConstants.combo_command, corbaCommand.getStringValue()); runCorba(); } }); } private void initCorbaServer() { if (initTable == true) { // return; } table.removeAll(); for (int i = 0; i < Integer.parseInt((String) properties.get("servicecount")); i++) { String index = Integer.toString(i); String name = (String) properties.get("service" + i + ".name"); String clazz = (String) properties.get("service" + i + ".class"); TableItem item = new TableItem(table, i); item.setText(new String[] { index, name, clazz, "Y" }); } initTable = true; } // private void addSecondSection(Composite parent) { // //Composite composite = createDefaultComposite(parent); // //Group groupHome = new Group(composite, 1); // Group groupCorba = new Group(parent, 1); // groupCorba.setText("Corba ÉèÖÃ"); // GridData gd = new GridData(GridData.FILL_BOTH); // groupCorba.setLayoutData(gd); // Corba_Home = new DirectoryFieldEditor(PreferenceConstants.P_PATH_COMBOHOME, "&Corba_Home:", groupCorba); // addField(Corba_Home); // // // addSeparator(composite); // //Composite compositeCommand = createDefaultComposite(parent); // //Group groupCommand = new Group(compositeCommand, 1); //// Group groupCommand = new Group(parent, 1); //// groupCommand.setText("Corba CommandÉèÖÃ"); //// GridData gdCommand = new GridData(GridData.FILL_BOTH); //// groupCommand.setLayoutData(gdCommand); // final StringFieldEditor corbaCommand = new StringFieldEditor(PreferenceConstants.combo_command, "Corba ÃüÁî:", groupCorba); // addField(corbaCommand); // // //final DirectoryFieldEditor // javaHome = new DirectoryFieldEditor(PreferenceConstants.java_home, "Java home:", groupCorba); // addField(javaHome); // // Button buttonComba = new Button(groupCorba, SWT.PUSH); // buttonComba.setText("Æô¶¯Corba·þÎñ..."); // buttonComba.addSelectionListener(new SelectionAdapter() { // public void widgetSelected(SelectionEvent e) { // if (Corba_Home.getStringValue().equals("")) { // MessageDialog.openInformation(getShell(), "Ìáʾ", "ÇëÑ¡ÔñVCI_Corba·¾¶"); // return; // // } // getPreferenceStore().setValue(PreferenceConstants.P_PATH_COMBOHOME, Corba_Home.getStringValue()); // getPreferenceStore().setValue(PreferenceConstants.java_home, javaHome.getStringValue()); // getPreferenceStore().setValue(PreferenceConstants.combo_command, corbaCommand.getStringValue()); // // runComba(); // } // }); // // } // private Composite createDefaultComposite(Composite parent) { // Composite composite = new Composite(parent, SWT.NULL); // GridLayout layout = new GridLayout(); // layout.numColumns = 2; // composite.setLayout(layout); // // GridData data = new GridData(); // data.verticalAlignment = GridData.FILL_BOTH; // data.horizontalAlignment = GridData.FILL_BOTH; // composite.setLayoutData(data); // // return composite; // } private void addSeparator(Composite parent) { Label separator = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL); GridData gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; separator.setLayoutData(gridData); } private void runCorba() { try { setCombaRun(); Runtime.getRuntime().exec("cmd /c start " + getPreferenceStore().getString(PreferenceConstants.P_PATH_COMBOHOME) + "\\run.bat /k"); // p.waitFor(); } catch (Exception e) { e.printStackTrace(); } } private String getInstance() { String Instance = ""; if (url != null) { String[] urls = url.split(":"); Instance = urls[urls.length - 1]; } return Instance; } private void testDBConnect() throws SQLException { Connection conn = null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); String url = "jdbc:oracle:thin:@" + getPreferenceStore().getString(PreferenceConstants.P_STRING_URL_IP) + ":1521:" + getPreferenceStore().getString(PreferenceConstants.P_STRING_URL_INSTANCE); String user = getPreferenceStore().getString(PreferenceConstants.P_STRING_USER); String pwd = getPreferenceStore().getString(PreferenceConstants.P_STRING_PWD); conn = DriverManager.getConnection(url, user, pwd); conn.createStatement(); if (conn != null) { MessageDialog.openInformation(this.getShell(), "Ìáʾ", "Êý¾Ý¿âÁ¬½Ó³É¹¦"); } } catch (ClassNotFoundException e) { MessageDialog.openInformation(this.getShell(), "Ìáʾ", "Êý¾Ý¿âÁ¬½Óʧ°Ü" + "\n" + e.getMessage()); } catch (SQLException e) { MessageDialog.openInformation(this.getShell(), "Ìáʾ", "Êý¾Ý¿âÁ¬½Óʧ°Ü" + "\n" + e.getMessage()); } finally { if (conn != null) { conn.close(); } } } class SortedProperties extends Properties { private static final long serialVersionUID = 1L; private final LinkedHashSet