SWT (JFace) text editor implements code _java programming

Source: Internet
Author: User
Tags gettext readline sleep trim stringbuffer
The code is as follows:


Implementation 1:


Basiceditor.java


Copy Code code as follows:



Package Swt_jface.demo5;


Import Java.io.BufferedReader;


Import Java.io.File;


Import Java.io.FileReader;


Import Java.io.FileWriter;


Import java.io.IOException;


Import Org.eclipse.jface.resource.ImageDescriptor;


Import Org.eclipse.jface.resource.ImageRegistry;


Import Org.eclipse.swt.SWT;


Import Org.eclipse.swt.custom.StyledText;


Import org.eclipse.swt.events.ModifyEvent;


Import Org.eclipse.swt.events.ModifyListener;


Import Org.eclipse.swt.graphics.Font;


Import Org.eclipse.swt.graphics.Image;


Import Org.eclipse.swt.layout.GridData;


Import Org.eclipse.swt.layout.GridLayout;


Import Org.eclipse.swt.widgets.Display;


Import org.eclipse.swt.widgets.Event;


Import Org.eclipse.swt.widgets.FileDialog;


Import Org.eclipse.swt.widgets.Listener;


Import Org.eclipse.swt.widgets.Menu;


Import Org.eclipse.swt.widgets.MenuItem;


Import Org.eclipse.swt.widgets.MessageBox;


Import Org.eclipse.swt.widgets.Shell;


Import Org.eclipse.swt.widgets.ToolBar;


Import Org.eclipse.swt.widgets.ToolItem;


public class Basiceditor {





Display display = new display ();


Shell shell = new shell (display);


Styledtext text;


Boolean hasunsavedchanges;


File file;


Private String lastopendirectory;


public static final String app_name = "Basiceditor v1.0";


MenuItem miwrap = null;





Public Basiceditor () {





Shell.setlayout (New GridLayout ());


ToolBar ToolBar = new ToolBar (Shell, SWT. FLAT | Swt. right);


Toolitem tinew = new Toolitem (ToolBar, SWT. PUSH);


Tinew.settext ("&new");


Tinew.setimage (GetImage ("new.gif"));


Tinew.addlistener (SWT. Selection, New Listener () {


public void Handleevent (event event) {


if (Handlechangesbeforediscard ()) {


FILE = null;


Text.settext ("");


}


}


});





Toolitem Tiopen = new Toolitem (ToolBar, SWT. PUSH);


Tiopen.settext ("&open");


Tiopen.setimage (GetImage ("open.gif"));


Tiopen.addlistener (SWT. Selection, New Listener () {


public void Handleevent (event event) {


if (Handlechangesbeforediscard ())


Loadtextfromfile ();


}


});





Toolitem tisave = new Toolitem (ToolBar, SWT. PUSH);


Tisave.settext ("&save");


Tisave.setimage (GetImage ("save.gif"));


Tisave.addlistener (SWT. Selection, New Listener () {


public void Handleevent (event event) {


Savetexttofile ();


}


});





New Toolitem (ToolBar, SWT. SEPARATOR);





Toolitem ticopy = new Toolitem (ToolBar, SWT. PUSH);


Ticopy.settext ("&copy");


Ticopy.setimage (GetImage ("copy.gif"));


Ticopy.addlistener (SWT. Selection, New Listener () {


public void Handleevent (event event) {


Text.copy ();


}


});





Toolitem ticut = new Toolitem (ToolBar, SWT. PUSH);


Ticut.settext ("cu&t");


Ticut.setimage (GetImage ("cut.gif"));


Ticut.addlistener (SWT. Selection, New Listener () {


public void Handleevent (event event) {


Text.cut ();


}


});





Toolitem tipaste = new Toolitem (ToolBar, SWT. PUSH);


Tipaste.settext ("&paste");


Tipaste.setimage (GetImage ("paste.gif"));


Tipaste.addlistener (SWT. Selection, New Listener () {


public void Handleevent (event event) {


Text.paste ();


}


});





New Toolitem (ToolBar, SWT. SEPARATOR);





Final Toolitem tiwrap = new Toolitem (ToolBar, SWT. CHECK);


Tiwrap.settext ("&wrap");


Tiwrap.addlistener (SWT. Selection, New Listener () {


public void Handleevent (event event) {


Text.setwordwrap (Tiwrap.getselection ());


Miwrap.setselection (Tiwrap.getselection ());


}


});





Toolbar.pack ();





System.out.println ("Client area:" + Shell.getclientarea ());





Text =


New Styledtext (


Shell


Swt. MULTI


| Swt. WRAP


| Swt. BORDER


| Swt. H_scroll


| Swt. V_scroll);


Text.setlayoutdata (New Griddata (Griddata.fill_both));


Font font = new Font (Shell.getdisplay (), "Courier new", SWT. NORMAL);


Text.setfont (font);


Text.settext ("Basiceditor version 1.0\r\nwriten by Jack Li Guojie.");


Text.addmodifylistener (New Modifylistener () {


public void Modifytext (Modifyevent e) {


Hasunsavedchanges = true;


}


});





Menu MenuBar = new Menu (Shell, SWT. BAR);





MenuItem Filemenuitem = new MenuItem (MenuBar, SWT. CASCADE);


Filemenuitem.settext ("&file");


Menu Filemenu = new Menu (Shell, SWT. Drop_down);





MenuItem minew = new MenuItem (Filemenu, SWT. PUSH);


Minew.settext ("&new\tctrl+n");


Minew.setimage (GetImage ("new.gif"));


Minew.setaccelerator (SWT. CTRL + ' N ');


Minew.addlistener (SWT. Selection, New Listener () {


public void Handleevent (event event) {


if (Handlechangesbeforediscard ()) {


FILE = null;


Text.settext ("");


}


}


});





MenuItem Miopen = new MenuItem (Filemenu, SWT. PUSH);


Miopen.settext ("&open\tctrl+o");


Miopen.setaccelerator (SWT. CTRL + ' O ');


Miopen.setimage (GetImage ("open.gif"));


Miopen.addlistener (SWT. Selection, New Listener () {


public void Handleevent (event event) {


if (Handlechangesbeforediscard ())


Loadtextfromfile ();


}


});





MenuItem misave = new MenuItem (Filemenu, SWT. PUSH);


Misave.settext ("&save\tctrl+s");


Misave.setimage (GetImage ("save.gif"));


Misave.setaccelerator (SWT. CTRL + ' S ');


Misave.addlistener (SWT. Selection, New Listener () {


public void Handleevent (event event) {


Savetexttofile ();


}


});





New MenuItem (Filemenu, SWT. SEPARATOR);





MenuItem miexit = new MenuItem (Filemenu, SWT. PUSH);


Miexit.settext ("&exit");


Miexit.addlistener (SWT. Selection, New Listener () {


public void Handleevent (event event) {


if (Handlechangesbeforediscard ())


Shell.dispose ();


}


});





Filemenuitem.setmenu (Filemenu);





MenuItem Editmenuitem = new MenuItem (MenuBar, SWT. CASCADE);


Editmenuitem.settext ("&edit");





Menu Editmenu = new Menu (Shell, SWT. Drop_down);





MenuItem micopy = new MenuItem (Editmenu, SWT. PUSH);


Micopy.settext ("&copy\tctrl+c");


Micopy.setimage (GetImage ("copy.gif"));


Micopy.setaccelerator (SWT. CTRL + ' C ');


Micopy.addlistener (SWT. Selection, New Listener () {


public void Handleevent (event event) {


Text.copy ();


}


});





MenuItem micut = new MenuItem (Editmenu, SWT. PUSH);


Micut.settext ("cu&t\tctrl+x");


Micut.setimage (GetImage ("cut.gif"));


Micut.setaccelerator (SWT. CTRL + ' X ');


Micut.addlistener (SWT. Selection, New Listener () {


public void Handleevent (event event) {


Text.cut ();


}


});





MenuItem mipaste = new MenuItem (Editmenu, SWT. PUSH);


Mipaste.settext ("&paste\tctrl+p");


Mipaste.setimage (GetImage ("paste.gif"));


Mipaste.setaccelerator (SWT. CTRL + ' P ');


Mipaste.addlistener (SWT. Selection, New Listener () {


public void Handleevent (event event) {


Text.paste ();


}


});





Editmenuitem.setmenu (Editmenu);





MenuItem Formatmenuitem = new MenuItem (MenuBar, SWT. CASCADE);


Formatmenuitem.settext ("&format");





Menu Formatmenu = new Menu (Shell, SWT. Drop_down);





Miwrap = new MenuItem (Formatmenu, SWT. CHECK);


Miwrap.settext ("&wrap\tctrl+w");


Miwrap.setaccelerator (SWT. CTRL + ' W ');


Miwrap.addlistener (SWT. Selection, New Listener () {


public void Handleevent (event event) {


Text.setwordwrap (Miwrap.getselection ());


Tiwrap.setselection (Miwrap.getselection ());


}


});





Formatmenuitem.setmenu (Formatmenu);





Shell.setmenubar (MenuBar);





Shell.setsize (400, 200);


Shell.open ();


while (!shell.isdisposed ()) {


if (!display.readanddispatch ()) {


Display.sleep ();


}


}


Display.dispose ();


}





Imageregistry imageregistry = new Imageregistry ();





Private Image GetImage (String shortfilename) {


if (Imageregistry.getdescriptor (shortfilename) = = null) {


Imagedescriptor descriptor = Imagedescriptor.createfromfile (null, "c:/icons/" + shortfilename);


Imageregistry.put (ShortFileName, descriptor);


}


Return Imageregistry.get (ShortFileName);


}


Boolean Handlechangesbeforediscard () {


if (! hasunsavedchanges)


return true;


MessageBox MessageBox = new MessageBox (Shell, SWT. icon_warning | Swt. YES | swt.no | Swt. CANCEL);


Messagebox.setmessage ("Do your want to save the changes to" + (file = = null?) A file? ": File.getname ());


Messagebox.settext (app_name);


int ret = Messagebox.open ();


if (ret = SWT.) YES) {


return Savetexttofile ();


}else if (ret = swt.no) {


return true;


}else{


return false;


}


}


Boolean loadtextfromfile () {


FileDialog dialog = new FileDialog (Shell, SWT. OPEN);


if (lastopendirectory!= null)


Dialog.setfilterpath (lastopendirectory);


String selectedfile = Dialog.open ();


if (selectedfile = = null) {


Log ("Action cancelled:loading the text from a file");


return false;


}


File = new file (selectedfile);


Lastopendirectory = File.getparent ();


try {


BufferedReader reader = new BufferedReader (new FileReader (file));


StringBuffer sb = new StringBuffer ();


String line = null;


while (line = Reader.readline ())!= null) {


Sb.append (line);


Sb.append ("\ r \ n");


}


Text.settext (Sb.tostring ());


return true;


}catch (IOException e) {


Log ("Failed to Load", "The text from File:" + file);


Log (e.tostring ());


}


return false;


}


Boolean savetexttofile () {


if (file = = null) {


FileDialog dialog = new FileDialog (Shell, SWT. SAVE);


if (lastopendirectory!= null)


Dialog.setfilterpath (lastopendirectory);


String selectedfile = Dialog.open ();


if (selectedfile = = null) {


Log ("Action cancelled:saving the text to a file");


return false;


}


File = new file (selectedfile);


Lastopendirectory = File.getparent ();


}


try {


FileWriter writer = new FileWriter (file);


Writer.write (Text.gettext ());


Writer.close ();


Log ("The text has been saved to file:" + file);


Hasunsavedchanges = false;


return true;


catch (IOException e) {


Log ("Failed to Save": "+ file");


Log (e.tostring ());


}


return false;


}


void log (String message) {


SYSTEM.OUT.PRINTLN (message);


}


public static void Main (string[] args) {


New Basiceditor ();


}


}





Implementation 2:


Basiceditor2.java


Copy Code code as follows:



Package Swt_jface.demo5;


Import Java.io.BufferedReader;


Import Java.io.File;


Import Java.io.FileReader;


Import Java.io.FileWriter;


Import java.io.IOException;


Import org.eclipse.jface.action.Action;


Import org.eclipse.jface.action.IAction;


Import Org.eclipse.jface.action.MenuManager;


Import Org.eclipse.jface.action.Separator;


Import Org.eclipse.jface.action.ToolBarManager;


Import Org.eclipse.jface.resource.ImageDescriptor;


Import Org.eclipse.jface.resource.ImageRegistry;


Import Org.eclipse.swt.SWT;


Import Org.eclipse.swt.custom.StyledText;


Import org.eclipse.swt.events.ModifyEvent;


Import Org.eclipse.swt.events.ModifyListener;


Import Org.eclipse.swt.graphics.Font;


Import Org.eclipse.swt.graphics.Image;


Import Org.eclipse.swt.layout.GridData;


Import Org.eclipse.swt.layout.GridLayout;


Import org.eclipse.swt.widgets.Decorations;


Import Org.eclipse.swt.widgets.Display;


Import Org.eclipse.swt.widgets.FileDialog;


Import Org.eclipse.swt.widgets.MenuItem;


Import Org.eclipse.swt.widgets.MessageBox;


Import Org.eclipse.swt.widgets.Shell;


Import Org.eclipse.swt.widgets.ToolBar;


public class BasicEditor2 {





Display display = new display ();


Shell shell = new shell (display);


Styledtext text;


Boolean hasunsavedchanges;


File file;


Private String lastopendirectory;


public static final String app_name = "Basiceditor v2.0";


MenuItem miwrap = null;


Public BasicEditor2 () {





Action actionnew =


New Action (


"&new",


Imagedescriptor.createfromfile (NULL, "C:/icons/new.gif")) {


public void Run () {


if (Handlechangesbeforediscard ()) {


FILE = null;


Text.settext ("");


}


}


};


Actionnew.setaccelerator (SWT. CTRL + ' N ');


Action Actionopen =


New Action (


"&open",


Imagedescriptor.createfromfile (NULL, "C:/icons/open.gif")) {


public void Run () {


if (Handlechangesbeforediscard ())


Loadtextfromfile ();


}


};


Actionopen.setaccelerator (SWT. CTRL + ' O ');





Action Actionsave =


New Action (


"&save\tctrl+s",


Imagedescriptor.createfromfile (NULL, "C:/icons/save.gif")) {


public void Run () {


Savetexttofile ();


}


};


Actionsave.setaccelerator (SWT. CTRL + ' S ');





Action actioncopy =


New Action (


"&copy",


Imagedescriptor.createfromfile (NULL, "C:/icons/copy.gif")) {


public void Run () {


Text.copy ();


}


};


Actioncopy.setaccelerator (SWT. CTRL + ' C ');





Action Actioncut =


New Action (


"Cu&t",


Imagedescriptor.createfromfile (NULL, "C:/icons/cut.gif")) {


public void Run () {


Text.cut ();


}


};


Actioncut.setaccelerator (SWT. CTRL + ' X ');





Action Actionpaste =


New Action (


"&paste",


Imagedescriptor.createfromfile (NULL, "C:/icons/paste.gif")) {


public void Run () {


Text.paste ();


}


};


Actionpaste.setaccelerator (SWT. CTRL + ' P ');





Action Actionwrap =


New Action (


"&wrap", Iaction.as_check_box) {


public void Run () {


Text.setwordwrap (ischecked ());


}


};


Actionwrap.setaccelerator (SWT. CTRL + ' W ');





Action Actionexit = new Action ("&exit@ctrl+x") {


public void Run () {


if (Handlechangesbeforediscard ())


Shell.dispose ();


}


};


System.out.println (Actionwrap.gettext ());





ToolBar ToolBar = new ToolBar (Shell, SWT. FLAT | Swt. right);


Toolbarmanager Toolbarmanager = new Toolbarmanager (ToolBar);


Toolbarmanager.add (actionnew);


Toolbarmanager.add (Actionopen);


Toolbarmanager.add (Actionsave);


Toolbarmanager.add (New Separator ());


Toolbarmanager.add (actioncopy);


Toolbarmanager.add (Actioncut);


Toolbarmanager.add (Actionpaste);


Toolbarmanager.add (New Separator ());


Toolbarmanager.add (Actionwrap);


Toolbarmanager.update (TRUE);


Shell.setlayout (New GridLayout ());


System.out.println ("Client area:" + Shell.getclientarea ());


Text =


New Styledtext (


Shell


Swt. MULTI


| Swt. WRAP


| Swt. BORDER


| Swt. H_scroll


| Swt. V_scroll);


Text.setlayoutdata (New Griddata (Griddata.fill_both));


Font font = new Font (Shell.getdisplay (), "Courier new", SWT. NORMAL);


Text.setfont (font);


Text.settext ("Basiceditor version 1.0\r\nwriten by Jack Li Guojie.");


Text.addmodifylistener (New Modifylistener () {


public void Modifytext (Modifyevent e) {


Hasunsavedchanges = true;


}


});


Menumanager Barmenumanager = new Menumanager ();





Menumanager Filemenumanager = new Menumanager ("&file");


Menumanager Editmenumanager = new Menumanager ("&edit");


Menumanager Formatmenumanager = new Menumanager ("&format");


Barmenumanager.add (Filemenumanager);


Barmenumanager.add (Editmenumanager);


Barmenumanager.add (Formatmenumanager);


Filemenumanager.add (actionnew);


Filemenumanager.add (Actionopen);


Filemenumanager.add (Actionsave);


Filemenumanager.add (New Separator ());


Filemenumanager.add (Actionexit);


Editmenumanager.add (actioncopy);


Editmenumanager.add (Actioncut);


Editmenumanager.add (Actionpaste);


Formatmenumanager.add (Actionwrap);


Shell.setmenubar (MenuBar);


Barmenumanager.updateall (TRUE);


Shell.setmenubar (Barmenumanager.createmenubar (decorations) shell);


Shell.setsize (400, 200);


Shell.open ();


while (!shell.isdisposed ()) {


if (!display.readanddispatch ()) {


Display.sleep ();


}


}


Display.dispose ();


}


Imageregistry imageregistry = new Imageregistry ();


Private Image GetImage (String shortfilename) {


if (Imageregistry.getdescriptor (shortfilename) = = null) {


Imagedescriptor descriptor =


Imagedescriptor.createfromfile (NULL, "c:/icons/" + shortfilename);


Imageregistry.put (ShortFileName, descriptor);


}


Return Imageregistry.get (ShortFileName);


}


Boolean Handlechangesbeforediscard () {


if (!hasunsavedchanges)


return true;


MessageBox MessageBox =


New MessageBox (


Shell


Swt. icon_warning | Swt. YES | swt.no | Swt. CANCEL);


Messagebox.setmessage (


"Do your want to save the changes"


+ (file = = null?) "A file?": File.getname ());


Messagebox.settext (app_name);


int ret = Messagebox.open ();


if (ret = SWT.) YES) {


return Savetexttofile ();


else if (ret = = swt.no) {


return true;


} else {


return false;


}


}


Boolean loadtextfromfile () {


FileDialog dialog = new FileDialog (Shell, SWT. OPEN);


if (lastopendirectory!= null)


Dialog.setfilterpath (lastopendirectory);


String selectedfile = Dialog.open ();


if (selectedfile = = null) {


Log ("Action cancelled:loading the text from a file");


return false;


}


File = new file (selectedfile);


Lastopendirectory = File.getparent ();


try {


BufferedReader reader = new BufferedReader (new FileReader (file));


StringBuffer sb = new StringBuffer ();


String line = null;


while (line = Reader.readline ())!= null) {


Sb.append (line);


Sb.append ("\ r \ n");


}


Text.settext (Sb.tostring ());


return true;


catch (IOException e) {


Log ("Failed to Load", "The text from File:" + file);


Log (e.tostring ());


}


return false;


}


Boolean savetexttofile () {


if (file = = null) {


FileDialog dialog = new FileDialog (Shell, SWT. SAVE);


if (lastopendirectory!= null)


Dialog.setfilterpath (lastopendirectory);


String selectedfile = Dialog.open ();


if (selectedfile = = null) {


Log ("Action cancelled:saving the text to a file");


return false;


}


File = new file (selectedfile);


Lastopendirectory = File.getparent ();


}


try {


FileWriter writer = new FileWriter (file);


Writer.write (Text.gettext ());


Writer.close ();


Log ("The text has been saved to file:" + file);


Hasunsavedchanges = false;


return true;


catch (IOException e) {


Log ("Failed to Save": "+ file");


Log (e.tostring ());


}


return false;


}


void log (String message) {


SYSTEM.OUT.PRINTLN (message);


}


public static void Main (string[] args) {


New BasicEditor2 ();


}


}





Implementation 3:


Basiceditor3.java


Copy Code code as follows:



Package swt_jface.demo11;


Import Java.io.BufferedReader;


Import Java.io.File;


Import Java.io.FileReader;


Import Java.io.FileWriter;


Import java.io.IOException;


Import org.eclipse.jface.action.Action;


Import org.eclipse.jface.action.IAction;


Import Org.eclipse.jface.action.MenuManager;


Import Org.eclipse.jface.action.Separator;


Import Org.eclipse.jface.action.ToolBarManager;


Import Org.eclipse.jface.resource.ImageDescriptor;


Import Org.eclipse.jface.resource.ImageRegistry;


Import Org.eclipse.swt.SWT;


Import Org.eclipse.swt.custom.StyledText;


Import org.eclipse.swt.custom.StyledTextPrintOptions;


Import org.eclipse.swt.events.ModifyEvent;


Import Org.eclipse.swt.events.ModifyListener;


Import Org.eclipse.swt.graphics.Font;


Import Org.eclipse.swt.graphics.GC;


Import Org.eclipse.swt.layout.GridData;


Import Org.eclipse.swt.layout.GridLayout;


Import Org.eclipse.swt.printing.PrintDialog;


Import Org.eclipse.swt.printing.Printer;


Import Org.eclipse.swt.printing.PrinterData;


Import org.eclipse.swt.widgets.Decorations;


Import Org.eclipse.swt.widgets.Display;


Import Org.eclipse.swt.widgets.FileDialog;


Import Org.eclipse.swt.widgets.MenuItem;


Import Org.eclipse.swt.widgets.MessageBox;


Import Org.eclipse.swt.widgets.Shell;


Import Org.eclipse.swt.widgets.ToolBar;


public class BasicEditor3 {





Display display = new display ();


Shell shell = new shell (display);


Styledtext text;


Boolean hasunsavedchanges;


File file;


Private String lastopendirectory;


public static final String app_name = "Basiceditor v3.0";


MenuItem miwrap = null;


Public BasicEditor3 () {


Action actionnew =


New Action (


"&new",


Imagedescriptor.createfromfile (NULL, "C:/icons/new.gif")) {


public void Run () {


if (Handlechangesbeforediscard ()) {


FILE = null;


Text.settext ("");


}


}


};


Actionnew.setaccelerator (SWT. CTRL + ' N ');


Action Actionopen =


New Action (


"&open",


Imagedescriptor.createfromfile (NULL, "C:/icons/open.gif")) {


public void Run () {


if (Handlechangesbeforediscard ())


Loadtextfromfile ();


}


};


Actionopen.setaccelerator (SWT. CTRL + ' O ');


Action Actionsave =


New Action (


"&save\tctrl+s",


Imagedescriptor.createfromfile (NULL, "C:/icons/save.gif")) {


public void Run () {


Savetexttofile ();


}


};


Actionsave.setaccelerator (SWT. CTRL + ' S ');


Action actioncopy =


New Action (


"&copy",


Imagedescriptor.createfromfile (NULL, "C:/icons/copy.gif")) {


public void Run () {


Text.copy ();


}


};


Actioncopy.setaccelerator (SWT. CTRL + ' C ');


Action Actioncut =


New Action (


"Cu&t",


Imagedescriptor.createfromfile (NULL, "C:/icons/cut.gif")) {


public void Run () {


Text.cut ();


}


};


Actioncut.setaccelerator (SWT. CTRL + ' X ');


Action Actionpaste =


New Action (


"&paste",


Imagedescriptor.createfromfile (NULL, "C:/icons/paste.gif")) {


public void Run () {


Text.paste ();


}


};


Actionpaste.setaccelerator (SWT. CTRL + ' P ');


Action Actionwrap =


New Action (


"&wrap", Iaction.as_check_box) {


public void Run () {


Text.setwordwrap (ischecked ());


}


};


Actionwrap.setaccelerator (SWT. CTRL + ' W ');


Action Actionexit = new Action ("&exit@ctrl+x") {


public void Run () {


if (Handlechangesbeforediscard ())


Shell.dispose ();


}


};


Action Actionprint = new Action ("&print@ctrl+p") {


public void Run () {


Printtext (Text.gettext ());


}


};


Action ActionPrint2 = new Action ("Print (Styledtext)") {


public void Run () {


Styledtextprintoptions options = new Styledtextprintoptions ();


Options.header = "SWT";


Options.footer = "Page <page>";


Options.jobname = "Text";





Runnable Runnable = text.print (New Printer (), options);


Runnable.run ();


}


};





ToolBar ToolBar = new ToolBar (Shell, SWT. FLAT | Swt. right);


Toolbarmanager Toolbarmanager = new Toolbarmanager (ToolBar);





Toolbarmanager.add (actionnew);


Toolbarmanager.add (Actionopen);


Toolbarmanager.add (Actionsave);


Toolbarmanager.add (New Separator ());


Toolbarmanager.add (actioncopy);


Toolbarmanager.add (Actioncut);


Toolbarmanager.add (Actionpaste);


Toolbarmanager.add (New Separator ());


Toolbarmanager.add (Actionwrap);


Toolbarmanager.add (New Separator ());


Toolbarmanager.add (Actionprint);


Toolbarmanager.add (ActionPrint2);


Toolbarmanager.update (TRUE);


Shell.settext (app_name);


Shell.setlayout (New GridLayout ());


Text =


New Styledtext (


Shell


Swt. MULTI


| Swt. WRAP


| Swt. BORDER


| Swt. H_scroll


| Swt. V_scroll);


Text.setlayoutdata (New Griddata (Griddata.fill_both));


Font font = new Font (Shell.getdisplay (), "Courier new", SWT. NORMAL);


Text.setfont (font);


Text.settext ("Basiceditor version 3.0\r\nwriten by Jack Li Guojie.");


Text.addmodifylistener (New Modifylistener () {


public void Modifytext (Modifyevent e) {


Hasunsavedchanges = true;


}


});


Menumanager Barmenumanager = new Menumanager ();


Menumanager Filemenumanager = new Menumanager ("&file");


Menumanager Editmenumanager = new Menumanager ("&edit");


Menumanager Formatmenumanager = new Menumanager ("&format");


Barmenumanager.add (Filemenumanager);


Barmenumanager.add (Editmenumanager);


Barmenumanager.add (Formatmenumanager);


Filemenumanager.add (actionnew);


Filemenumanager.add (Actionopen);


Filemenumanager.add (Actionsave);


Filemenumanager.add (Actionprint);


Filemenumanager.add (New Separator ());


Filemenumanager.add (Actionexit);


Editmenumanager.add (actioncopy);


Editmenumanager.add (Actioncut);


Editmenumanager.add (Actionpaste);





Formatmenumanager.add (Actionwrap);


Shell.setmenubar (MenuBar);


Barmenumanager.updateall (TRUE);


Shell.setmenubar (Barmenumanager.createmenubar (decorations) shell);


Shell.setsize (400, 200);


Shell.open ();


while (!shell.isdisposed ()) {


if (!display.readanddispatch ()) {


Display.sleep ();


}


}


Display.dispose ();


}





int x;


int y;


int lineheight;


Printmargin margins;


int pagenumber = 1;


int linenumber = 1;





void Printtext (String text) {


PrintDialog dialog = new PrintDialog (shell);


Printerdata printerdata = Dialog.open ();


if (Printerdata = null) return;


Printer Printer = new Printer (printerdata);


if (! Printer.startjob ("text")) return;


GC GC = new GC (printer);


margins = Printmargin.getprintmargin (printer, 1.0);


x = Margins.left;


y = margins.top;


StringBuffer buffer = new StringBuffer ();


Font font = new Font (printer, Arial, SWT). NORMAL);


Gc.setfont (font);


Lineheight = Gc.getfontmetrics (). GetHeight ();


Printer.startpage ();


String page = "-" + pagenumber + "-";


Gc.drawstring (page, margins.right-margins.left-gc.textextent (page). x)/2 + margins.left, Margins.bottom + gc.textExt ENT (page). Y);


for (int index = 0; index <text.length ();) {


char C = Text.charat (index);


Switch (c) {


Case ' \ R ':


if (Index < Text.length ()-1 && text.charat (index + 1) = = ' \ n ') {


Printnewline (printer, GC, buffer.tostring ());


Buffer.setlength (0);


Index + 2;


}


Break


Case ' t ':


Case ':


if (Gc.textextent (buffer.tostring () + "). x > Margins.right-margins.left) {


Printnewline (printer, GC, buffer.tostring ());


Buffer.setlength (0);


}


Buffer.append (c);


if (Index < Text.length ()-1 && (! Character.iswhitespace (Text.charat (index + 1))) {//Looks up one word to the whether of the line should wraps.


String Word = Readword (text, index + 1);


if (Gc.textextent (buffer.tostring () + word). x > Margins.right-margins.left) {


Printnewline (printer, GC, buffer.tostring ());


Buffer.setlength (0);


}


}


Index + 1;


Break


Default


Buffer.append (c);


Index + 1;


}


}





if (buffer.length () > 0) printnewline (printer, GC, buffer.tostring ());


if (y + lineheight <= margins.bottom) printer.endpage ();


Printer.endjob ();


Gc.dispose ();


Font.dispose ();


Printer.dispose ();


}


void Printnewline (Printer Printer, GC GC, String Line) {


if (y + lineheight > Margins.bottom) {


Printer.endpage ();


x = Margins.left;


y = margins.top;


PageNumber + +;


linenumber = 1;


String page = "-" + pagenumber + "-";


Gc.drawstring (page, margins.right-margins.left-gc.textextent (page). x)/2 + margins.left, Margins.bottom + gc.textExt ENT (page). Y);


}


Gc.drawstring (line, x, y);


Y + + lineheight;


}


String Readword (string text, int offset) {


StringBuffer sb = new StringBuffer ();


int index = offset;


char c = 0;


while (Index < text.length ()) {


c = Text.charat (index);


if (Character.iswhitespace (c)) break;


Sb.append (c);


Index + 1;


}


return sb.tostring ();


}


Imageregistry imageregistry = new Imageregistry ();


Private Image GetImage (String shortfilename) {


if (Imageregistry.getdescriptor (shortfilename) = = null) {


Imagedescriptor descriptor =


Imagedescriptor.createfromfile (NULL, "c:/icons/" + shortfilename);


Imageregistry.put (ShortFileName, descriptor);


//        }


Return Imageregistry.get (ShortFileName);


//    }


Boolean Handlechangesbeforediscard () {


if (!hasunsavedchanges)


return true;


MessageBox MessageBox =


New MessageBox (


Shell


Swt. icon_warning | Swt. YES | swt.no | Swt. CANCEL);


Messagebox.setmessage (


"Do your want to save the changes"


+ (file = = null?) "A file?": File.getname ());


Messagebox.settext (app_name);


int ret = Messagebox.open ();


if (ret = SWT.) YES) {


return Savetexttofile ();


else if (ret = = swt.no) {


return true;


} else {


return false;


}


}


Boolean loadtextfromfile () {


FileDialog dialog = new FileDialog (Shell, SWT. OPEN);


if (lastopendirectory!= null)


Dialog.setfilterpath (lastopendirectory);


String selectedfile = Dialog.open ();


if (selectedfile = = null) {


Log ("Action cancelled:loading the text from a file");


return false;


}


File = new file (selectedfile);


Lastopendirectory = File.getparent ();


try {


BufferedReader reader = new BufferedReader (new FileReader (file));


StringBuffer sb = new StringBuffer ();


String line = null;


while (line = Reader.readline ())!= null) {


Sb.append (line);


Sb.append ("\ r \ n");


}


Text.settext (Sb.tostring ());


return true;


catch (IOException e) {


Log ("Failed to Load", "The text from File:" + file);


Log (e.tostring ());


}


return false;


}


Boolean savetexttofile () {


if (file = = null) {


FileDialog dialog = new FileDialog (Shell, SWT. SAVE);


if (lastopendirectory!= null)


Dialog.setfilterpath (lastopendirectory);


String selectedfile = Dialog.open ();


if (selectedfile = = null) {


Log ("Action cancelled:saving the text to a file");


return false;


}


File = new file (selectedfile);


Lastopendirectory = File.getparent ();


}


try {


FileWriter writer = new FileWriter (file);


Writer.write (Text.gettext ());


Writer.close ();


Log ("The text has been saved to file:" + file);


Hasunsavedchanges = false;


return true;


catch (IOException e) {


Log ("Failed to Save": "+ file");


Log (e.tostring ());


}


return false;


}


void log (String message) {


SYSTEM.OUT.PRINTLN (message);


}


public static void Main (string[] args) {


New BasicEditor3 ();


}


}





The printer can be appended with the following class:


Copy Code code as follows:



Package swt_jface.demo11;


Import Org.eclipse.swt.graphics.Point;


Import Org.eclipse.swt.graphics.Rectangle;


Import Org.eclipse.swt.printing.Printer;


public class Printmargin {





public int left;


public int right;


public int top;


public int bottom;


Private Printmargin (int left, right of int, int top, int bottom) {


This.left = left;


This.right = right;


This.top = top;


This.bottom = bottom;


}


Static Printmargin Getprintmargin (Printer Printer, double margin) {


return Getprintmargin (printer, margin, margin, margin, margin);


}


Static Printmargin Getprintmargin (


Printer Printer,


Double MarginLeft,


Double MarginRight,


Double MarginTop,


Double marginbottom) {


Rectangle Clientarea = Printer.getclientarea ();


Rectangle trim = Printer.computetrim (0, 0, 0, 0);


Point dpi = printer.getdpi ();


int leftMargin = (int) (MARGINLEFT * dpi.x)-trim.x;


int rightMargin =


Clientarea.width


+ Trim.width


-(int) (MarginRight * dpi.x)


-Trim.x;


int topMargin = (int) (MARGINTOP * dpi.y)-trim.y;


int bottommargin =


Clientarea.height


+ Trim.height


-(int) (MarginBottom * dpi.y)


-Trim.y;


return new Printmargin (


LeftMargin,


RightMargin,


TopMargin,


BottomMargin);


}


Public String toString () {


Return "Margin {"


+ Left


+ ", "


+ Right


+ "; "


+ Top


+ ", "


+ Bottom


+ " }";


}


}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.