SWT Security save dialog box safesavedialog

Source: Internet
Author: User
There is no direct secure save dialog box for SWT & jface. This article implements the security dialog box and sets the Save type.

Package org. gridhome. UI. window;

Import java. Io. file;

Import org. Eclipse. SWT. SWT;
Import org. Eclipse. SWT. Events. selectionadapter;
Import org. Eclipse. SWT. Events. selectionevent;
Import org. Eclipse. SWT. layout. gridlayout;
Import org. Eclipse. SWT. Widgets. Button;
Import org. Eclipse. SWT. Widgets. display;
Import org. Eclipse. SWT. Widgets. filedialog;
Import org. Eclipse. SWT. Widgets. MessageBox;
Import org. Eclipse. SWT. Widgets. shell;

Public class safesavedialog {
Public safesavedialog (){
}
Filedialog DLG;

Public safesavedialog (shell ){
DLG = new filedialog (shell, SWT. Save); // set it to the Save dialog box
// Set the storage type
DLG. setfilternames (New String [] {
"Text files (*. txt )",
"SQL files (*. SQL )",
"All files (*.*)"
});
DLG. setfilterextensions (New String [] {
"*. Txt", "*. SQL ","*.*"
});
}

Public String open (){
// Store the selected file name in Filename
String filename = NULL;

// The user has finished when one of
// Following happens:
// 1) The user dismisses the dialog by pressing cancel
// 2) the selected file name does not exist
// 3) The user agrees to overwrite existing file
Boolean done = false;

While (! Done ){
// Open the file dialog
Filename = DLG. open ();
If (filename = NULL ){
// User has canceled, so quit and return
Done = true;
} Else {
// User has selected a file; see if it already exists
File file = new file (filename );
If (file. exists ()){
// The file already exists; asks for confirmation
MessageBox MB = new MessageBox (DLG. getparent (),
SWT. icon_warning | SWT. Yes | SWT. No );
MB. settext ("Tips ");
MB. setmessage (filename
+ "Already exists. Do you want to replace it? ");

// If they click Yes, drop out. If they click no,
// Redisplay the File Dialog
Done = Mb. open () = SWT. Yes;
} Else {
// File does not exist, so drop out
Done = true;
}
}
}
Return filename;
}
Public String getfilename (){
Return DLG. getfilename ();
}

Public String [] getfilenames (){
Return DLG. getfilenames ();
}

Public String [] getfilterextensions (){
Return DLG. getfilterextensions ();
}

Public String [] getfilternames (){
Return DLG. getfilternames ();
}

Public String getfilterpath (){
Return DLG. getfilterpath ();
}

Public void setfilename (string ){
DLG. setfilename (string );
}

Public void setfilterextensions (string [] extensions ){
DLG. setfilterextensions (extensions );
}

Public void setfilternames (string [] Names ){
DLG. setfilternames (names );
}

Public void setfilterpath (string ){
DLG. setfilterpath (string );
}

Public shell getparent (){
Return DLG. getparent ();
}

Public int getstyle (){
Return DLG. getstyle ();
}

Public String gettext (){
Return DLG. gettext ();
}

Public void settext (string ){
DLG. settext (string );
}
/**
* Runs the application
*/
Public void run (){
Display display = New Display ();
Shell shell = new shell (Display );
Shell. settext ("Safe save ");
Createcontents (Shell );
Shell. Pack ();
Shell. open ();
While (! Shell. isdisposed ()){
If (! Display. readanddispatch ()){
Display. Sleep ();
}
}

Display. Dispose ();
}
/**
* Creates the window Contents
*
* @ Param shell the parent Shell
*/
Private void createcontents (final shell ){
Shell. setlayout (New gridlayout (2, false ));
Button button = new button (shell, SWT. Push );
Button. settext ("Save ...");
Button. addselectionlistener (New selectionadapter (){
Public void widgetselected (selectionevent event ){
// Create the color-change Dialog
Safesavedialog DLG = new safesavedialog (Shell );

DLG. open ();
// Call pack () to resize the window to fit the new font
Shell. Pack ();
}
});
}


Public static void main (string [] ARGs ){
New safesavedialog (). Run ();
}
}
 

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.