sqllistsection.java

来自「一个eclipse插件源代码。用于web开发」· Java 代码 · 共 398 行

JAVA
398
字号
/*
 * $Header: /home/cvs/WEBPUMP2.0/WebPumpIDE_Src/WebPumpIDE/src/com/webpump/ui/sql/SQLListSection.java,v 1.1.1.1 2004/07/01 09:07:52 wang_j Exp $
 * $Revision: 1.1.1.1 $
 * $Date: 2004/07/01 09:07:52 $
 *
 * ====================================================================
 *
 * The NanJing HopeRun(IT-FOREST) Software License, Version 2.0.0
 *
 * Copyright 2003-2004 by NanJing HopeRun(IT-FOREST) Information System Co., Ltd, CHINA and
 *                        IT Forest Corporation
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * HopeRun(IT-FOREST) Information System Co., Ltd, CHINA and IT Forest Corporation.
 * You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with
 * HopeRun(IT-FOREST) Information System Co., Ltd, CHINA and IT Forest Corporation.
 */
 
package com.webpump.ui.sql;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.pde.core.IModelChangedEvent;
import org.eclipse.swt.widgets.FileDialog;
import com.webpump.ui.base.data.BaseObject;
import com.webpump.ui.base.gui.BasePage;
import com.webpump.ui.base.gui.BaseTableSection;
import com.webpump.ui.perspective.MacroResource;
import com.webpump.ui.perspective.WebpumpIDEPlugin;
import com.webpump.ui.wizard.FileUtil;
import com.webpump.ui.dialog.*;
import org.eclipse.swt.SWT;


/**
 * Class for section to show SQL list.
 * 
 * @author luo_sa
 * @version 2.0.0 2004-2-24
 */
public class SQLListSection extends BaseTableSection {
    
    /** define the ModelChanged function work or not*/
    boolean m_bModelChangedListen = true;
    /**
     * Constructor which define the content of this part
     * @param page the page in which the part lies
     */ 
    public SQLListSection(BasePage page) {
        
        super(page, 
               new String[]{WebpumpIDEPlugin.getResourceString(MacroResource.ADDBUTTON), 
                            WebpumpIDEPlugin.getResourceString(MacroResource.DELBUTTON),
                            WebpumpIDEPlugin.getResourceString(MacroResource.SQLLISTSECTION_IMPORT),
                            WebpumpIDEPlugin.getResourceString(MacroResource.SQLLISTSECTION_EXPORT)
                            });
        setHeaderText(WebpumpIDEPlugin.getResourceString(MacroResource.SQLLISTSECTION_HEADER_TEXT));
        getTablePart().setEditable(false);
    }
    
    /**
     * Initialize the layout of this part.
     */
    public void initialize(Object input) {
        
        SQLModel objSQLModel = (SQLModel) input;
        tableViewer.setInput((SQLInfo) objSQLModel.getDataObject());
        objSQLModel.addModelChangedListener(this);
        getTablePart().setButtonEnabled(0, true);
        getTablePart().setButtonEnabled(1, false);      
    }
    
    /**
     * Define the action to the TableViewer after the selection is changed
     * 
     * @param selection the selected item
     */
    protected void selectionChanged(IStructuredSelection selection) {
        
        Object item = selection.getFirstElement();
        SQLSection objSqlSection = ((SQLForm) (getFormPage().getForm())).getSqlSection();
        objSqlSection.toShowSQL((KeyAndValue)item);
        PerformSection objPerformSection = ((SQLForm)(getFormPage().getForm())).getPerformSection();
        objPerformSection.clearTable();
        fireSelectionNotification((KeyAndValue) item);
        getFormPage().setSelection(selection);
        setButtonStyle(item);
        
    }
    
    
    protected void handleDoubleClick(IStructuredSelection selection)
    {
        Object object = selection.getFirstElement();
         
        if (object instanceof KeyAndValue)
        {
            String strValue = ((KeyAndValue) object).getKey();
            SQLInfo objSQLInfo = (SQLInfo) ((KeyAndValue) object).getParent();
            AddDialog objWizard = new AddDialog(WebpumpIDEPlugin.getResourceString(MacroResource.SQLLISTSECTION_WINDOW_TITLE),
                                                WebpumpIDEPlugin.getResourceString(MacroResource.SQLLISTSECTION_TITLE), 
                                                WebpumpIDEPlugin.getResourceString(MacroResource.SQLLISTSECTION_DESCRIPTION),
                                                WebpumpIDEPlugin.getResourceString(MacroResource.SQLLISTSECTION_LABEL_TEXT),
                                                strValue,
                                                objSQLInfo);
            
            objWizard.init(WebpumpIDEPlugin.getDefault().getWorkbench(), null);          

            WizardDialog objDialog = new WizardDialog(WebpumpIDEPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getShell(),objWizard);
            
            if (objDialog.open() == WizardDialog.OK )
            {
                String value = objWizard.getValue();
                ((KeyAndValue)object).setKey(value); 
            }  
            this.update();                     
                                                
        }
    }
    
    /** 
     * Define what to do when selecting one of the botton
     * 
     * @param index a param which indicates the botton which has been selected
     */
    protected void buttonSelected(int index) {
        
        if (index == 0)
            handleAdd();
        if (index == 1)
            handleRemove();
        if (index == 2)
            handleImport();
        if (index == 3)
            handleExport();                  
    }
    
    /** 
     * Define the action when having selected the "add" button
     */     
    private void handleAdd() {
        
        SQLInfo objControl = (SQLInfo) ((SQLModel)this.getFormPage().getEditor().getModel()).getDataObject();
        String strValue = "";
        AddDialog objWizard = new AddDialog(WebpumpIDEPlugin.getResourceString(MacroResource.SQLLISTSECTION_WINDOW_TITLE),
                                            WebpumpIDEPlugin.getResourceString(MacroResource.SQLLISTSECTION_TITLE), 
                                            WebpumpIDEPlugin.getResourceString(MacroResource.SQLLISTSECTION_DESCRIPTION),
                                            WebpumpIDEPlugin.getResourceString(MacroResource.SQLLISTSECTION_LABEL_TEXT),
                                            strValue,
                                            objControl);
            
        objWizard.init(WebpumpIDEPlugin.getDefault().getWorkbench(), null);          

        WizardDialog objDialog = new WizardDialog(WebpumpIDEPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getShell(),objWizard);
            
        if (objDialog.open() == WizardDialog.OK )
        {
            KeyAndValue objKeyAndValue = new KeyAndValue();    
            String value = objWizard.getValue();
            objKeyAndValue.setKeyNotFire(value);
            objControl.addKeyAndValue(objKeyAndValue);
            this.update();  
        }  
            
    }   
    
    /**
     * Define the action when having selected the "remove" button
     */
    private void handleRemove() {
        
        IStructuredSelection sel =
            (IStructuredSelection) tableViewer.getSelection();
        BaseObject object = (BaseObject) sel.getFirstElement();
        BaseObject objControl = null;
        
        if (object instanceof KeyAndValue) {
            objControl = object.getParent();
            ((SQLInfo) objControl).remove((KeyAndValue) object);
            this.update();
        }
               
    }   
    
    
    /**
     * Define the action when having selected the "import" button
     *
     */
    private void handleImport() {
        
        FileDialog dialog =
            new FileDialog(getFormPage().getEditor().getEditorSite().getShell(), SWT.OPEN);
        dialog.setFilterExtensions(new String[] { "*.csv" });
        dialog.setText(WebpumpIDEPlugin.getResourceString(MacroResource.SQLLISTSECTION_IMPORT_DIALOG_TEXT)); //$NON-NLS-1$

        String str=dialog.open();
        if(str!=null){
            loadFile(str);
        }
    }
    
    
    /**
     * Load the "csv" file and add to SQLModel object.
     * @param file
     */
    public void loadFile(String file) {
        m_bModelChangedListen = false;
        SQLInfo objSQLInfo = (SQLInfo) ((SQLModel) getFormPage().getEditor().getModel()).getDataObject();
        FileReader fr=null;
        BufferedReaderNew in=null;
        try{
            fr=new FileReader(file);
            in = new BufferedReaderNew(fr);
            String str;
            StringBuffer all=new StringBuffer();
            while ((str = in.readLine()) != null) {
                //all.append(str);
                if (str.length() > 0) {
                    str = str.trim();
                    int index = str.indexOf(',');
                    String key = str.substring(0, index);
                    key = key.trim();
                    String value = str.substring(index + 1, str.length());
                    value = value.replaceAll("\"","");
                    value = value.trim();
                    for(int i = 0; i < objSQLInfo.getChild().length; i++) {
                        KeyAndValue obj = (KeyAndValue) objSQLInfo.getChild()[i];
                        if (obj.getKey().compareTo(key) == 0) {
                            obj.setValue(value);
                            break;       
                        }
                        if (i == (objSQLInfo.getChild().length - 1)) {
                            KeyAndValue keyandvalue = new KeyAndValue();
                            objSQLInfo.addKeyAndValue(keyandvalue);
                            keyandvalue.setKeyAndValue(key, value);
                        }
                    }
                    if(objSQLInfo.getChild().length == 0){
                        KeyAndValue keyandvalue = new KeyAndValue();
                        objSQLInfo.addKeyAndValue(keyandvalue);
                        keyandvalue.setKeyAndValue(key, value);
                    }

                }
            
            }
            
        }catch(Throwable e){

        }
        finally {
            try {
                fr.close();
            } catch (java.io.IOException e) {
                WebpumpIDEPlugin.log(e);
            }
            try {
                in.close();               
            } catch (java.io.IOException e){
                WebpumpIDEPlugin.log(e);
            }

        }

        tableViewer.setInput(objSQLInfo);
        tableViewer.refresh();
        m_bModelChangedListen = true;

        
    }
    
    
    /**
     * Define the action when having selected the "export" button
     *
     */
    private void handleExport() {
        FileDialog dlg=new FileDialog(getFormPage().getEditor().getEditorSite().getShell(),SWT.SAVE);
            dlg.setFilterExtensions(new String[]{"*.csv"});  //$NON-NLS-1$ //$NON-NLS-2$
            dlg.setText(WebpumpIDEPlugin.getResourceString(MacroResource.SQLLISTSECTION_EXPORT_DIALOG_TEXT));
            String str=dlg.open();
            String filepath = null;

            if(str!=null){
                if (str.endsWith(".csv")) {
                    filepath = str;
                } else {
                    filepath = str.concat(".csv");
                }
                saveFile(filepath);
            }
            
    }
    
    
    /**
     * Save the SQLModel object in "csv" file.
     * @param file
     */
    public void saveFile(String file) {
        File csvfile = new File(file);
        try {
            String testString = ((SQLModel) getFormPage().getEditor().getModel()).getSQLModelString();
            System.out.println(testString);
            //写xml文??
            FileUtil.toTextFile(csvfile, ((SQLModel) getFormPage().getEditor().getModel()).getSQLModelString());
			MessageDialog.openInformation(getFormPage().getEditor().getEditorSite().getShell() ,
						WebpumpIDEPlugin.getResourceString(MacroResource.PERFORMBUTTONSELECTIONLISTENER_NOTE),
						WebpumpIDEPlugin.getResourceString(MacroResource.SQLLISTSECTION_SAVE_SUCCESS));

        }
        catch (IOException e) {
            WebpumpIDEPlugin.log(e);
			MessageDialog.openInformation(getFormPage().getEditor().getEditorSite().getShell() ,
						WebpumpIDEPlugin.getResourceString(MacroResource.PERFORMBUTTONSELECTIONLISTENER_NOTE),
						WebpumpIDEPlugin.getResourceString(MacroResource.SQLLISTSECTION_SAVE_FAIL));

        }
    }
    
    /**
     * Set the botton style according to the selection
     * @param item the selected item
     */
    public void setButtonStyle(Object item) {
        
        if (isReadOnly())
            return;
        boolean removeEnabled = false;
        
        if (item != null) {
            if (item instanceof KeyAndValue) {
                removeEnabled = true;                   
            }          
        }         
        
        getTablePart().setButtonEnabled(1, removeEnabled);       
        
    }
    
    
    /**
     * Define actions when model changed
     */
    public void modelChanged(IModelChangedEvent event) {
        
        //直接改变了文??
        if (event.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
            SQLModel model = (SQLModel) getFormPage().getModel();
            tableViewer.setInput((SQLInfo) model.getDataObject());
            tableViewer.refresh();
            return;
        }
        
        //Get the changed object
        Object changeObject = event.getChangedObjects()[0];
        //Define the action to the tableViewer if the changed object is an instance of ExceptionInfo
        if ((changeObject instanceof KeyAndValue) && m_bModelChangedListen) {
            KeyAndValue pobj = (KeyAndValue) changeObject;
            
            //model中某个??据对象发生了变化
            if(event.getChangeType() == IModelChangedEvent.CHANGE){
                return;
            }
            
            //model 中增铁菟一个??据对??
            if(event.getChangeType() == IModelChangedEvent.INSERT) {
                tableViewer.add(pobj);
                tableViewer.setSelection(new StructuredSelection(pobj), true);
                tableViewer.getTable().setFocus();  
            }
            
            //从model中删??了一个??据对??
            if(event.getChangeType() == IModelChangedEvent.REMOVE) {
                tableViewer.remove(pobj);
                
            }else{
                
                tableViewer.update(pobj, null);
            }
            
            setDirty(true);
        }         
    }
     

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?