OleClientSite addKeyListener似乎无法在java中使用swt

时间:2016-11-10 06:43:22

标签: java eclipse swt

我使用OleClientSite在java中打开swt中的word文档。现在我想在用户按ctrl + s时保存文档。我在OleClientSite中添加了一个keylistener。代码如下:

public class SWTMenuExample {
    static OleClientSite clientSite;
    static OleFrame frame;
    static File file;
    static Shell shell;
    static KeyListener keyListener;

    public static void main(final String[] args) {
        final Display display = new Display();
        shell = new Shell(display);

        shell.setSize(800, 600);
        shell.setText("Word Example");
        shell.setLayout(new FillLayout());

        try {
            frame = new OleFrame(shell, SWT.NONE);
            // esto abre un documento existente
            // clientSite = new OleClientSite(frame, SWT.NONE, new
            // File("Doc1.doc"));
            // esto abre un documento en blanco
            // clientSite = new OleClientSite(frame, SWT.NONE, "Word.Document");
            addFileMenu(frame);
            System.out.println(" I am in run method ");
        } catch (final SWTError e) {
            System.out.println("Unable to open activeX control");
            display.dispose();
            return;
        }
        keyListener = new KeyListener() {

            public void keyReleased(KeyEvent paramKeyEvent) {

            }

            public void keyPressed(KeyEvent paramKeyEvent) {
                // TODO Auto-generated method stub
                if (((paramKeyEvent.stateMask & SWT.CTRL) == SWT.CTRL)
                        && (paramKeyEvent.keyCode == 's')) {
                    JOptionPane.showMessageDialog(null,
                            "ctrl+s is pressed down initial ",
                            "Warning Message", JOptionPane.WARNING_MESSAGE);
                    if (file != null) {
                        clientSite.save(file, true);
                        fileSave();
                        JOptionPane.showMessageDialog(null,
                                "ctrl+s is pressed down", "Warning Message",
                                JOptionPane.WARNING_MESSAGE);
                    } else
                        JOptionPane.showMessageDialog(null, "File is null",
                                "Warning Message", JOptionPane.WARNING_MESSAGE);
                }

            }
        };




        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }

    static void addFileMenu(OleFrame frame) {
        final Shell shell = frame.getShell();
        Menu menuBar = shell.getMenuBar();
        if (menuBar == null) {
            menuBar = new Menu(shell, SWT.BAR);
            shell.setMenuBar(menuBar);
        }
        MenuItem fileMenu = new MenuItem(menuBar, SWT.CASCADE);
        fileMenu.setText("&File");
        Menu menuFile = new Menu(fileMenu);
        fileMenu.setMenu(menuFile);
        frame.setFileMenus(new MenuItem[] { fileMenu });

        MenuItem menuFileCreate = new MenuItem(menuFile, SWT.CASCADE);
        menuFileCreate.setText("Create File");
        menuFileCreate.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                fileCreate();
            }
        });

        MenuItem menuFileOpen = new MenuItem(menuFile, SWT.CASCADE);
        menuFileOpen.setText("Open File");
        menuFileOpen.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                fileOpen();
            }
        });

        MenuItem menuFileSave = new MenuItem(menuFile, SWT.CASCADE);
        menuFileSave.setText("Save File");
        menuFileSave.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                if (file != null) {
                    clientSite.save(file, true);
                    fileSave();
                }
            }
        });

        MenuItem menuFileClose = new MenuItem(menuFile, SWT.CASCADE);
        menuFileClose.setText("Close File");
        menuFileClose.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                if (clientSite != null) {
                    clientSite.dispose();
                    file = null;
                }
            }
        });
    }

    static void fileCreate() {
        String fileName = JOptionPane
                .showInputDialog("Please input the name of you file: ");
        if (fileName != null) {
            if (fileName.trim().equals("")) {
                JFrame frame = new JFrame(
                        "JOptionPane showMessageDialog example");

                JOptionPane.showMessageDialog(frame,
                        "File Name must have some value", "Warning Message",
                        JOptionPane.WARNING_MESSAGE);
            } else {

                Connection conn = DBConnectionHandler.getConnection();
                PreparedStatement pstmt;
                try {
                    pstmt = conn
                            .prepareStatement("insert into FILEDATA(PR_KEY, FILENAME, FILEDATA) values (seq_file.nextval,?, EMPTY_BLOB())");
                    pstmt.setString(1, fileName + ".docx");
                    pstmt.executeUpdate();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }

    static void fileOpen() {
        FileDialog dialog = new FileDialog(shell, SWT.OPEN);
        dialog.setFilterExtensions(new String[] { "*.docx" });
        String fileName = dialog.open();
        if (fileName != null) {
            if (clientSite != null)
                clientSite.dispose();
            file = new File(fileName);
            clientSite = new OleClientSite(frame, SWT.NONE, "Word.Document",
                    file);
            clientSite.addKeyListener(keyListener); 
            clientSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
        }
    }

    static void fileSave() {
        try {
            FileInputStream fis = new FileInputStream(file);
            Connection conn = DBConnectionHandler.getConnection();

            PreparedStatement pstmt = conn
                    .prepareStatement("update FILEDATA  set FILEDATA = ?  where FILENAME = ?");
            pstmt.setBinaryStream(1, fis, (int) file.length());
            pstmt.setString(2, file.getName());
            pstmt.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

但是keylistener无法正常工作。问题是什么 ?我怎么解决这个问题?请帮我 。

0 个答案:

没有答案