eclipse - OleClientSite addKeyListener seems not working in swt in java -
i opening word document in swt in java using oleclientsite . want save document when user press ctrl+s . have added keylistener oleclientsite . code follows :
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(" in run method "); } catch (final swterror e) { system.out.println("unable 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 pressed down initial ", "warning message", joptionpane.warning_message); if (file != null) { clientsite.save(file, true); filesave(); joptionpane.showmessagedialog(null, "ctrl+s pressed down", "warning message", joptionpane.warning_message); } else joptionpane.showmessagedialog(null, "file 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 name of file: "); if (filename != null) { if (filename.trim().equals("")) { jframe frame = new jframe( "joptionpane showmessagedialog example"); joptionpane.showmessagedialog(frame, "file name must have value", "warning message", joptionpane.warning_message); } else { connection conn = dbconnectionhandler.getconnection(); preparedstatement pstmt; try { pstmt = conn .preparestatement("insert 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 = ? 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(); } } }
but keylistener not working . problem ? how can solve ? please me .
Comments
Post a Comment