神奇代码备份恢复工具逸事与操作指南

阅读: 评论:0

神奇代码备份恢复工具逸事与操作指南

神奇代码备份恢复工具逸事与操作指南

文章目录

  • 一,序
  • 二,逸事
  • 三,为什么今天要提这个工具
  • 四,操作界面
    • 1. 文章发表者备份项目步骤
    • 2. 文章发表者恢复项目操作步骤
    • 3. 文章阅读者恢复项目步骤
  • 五,附件
    • 1. 示例备份文件
    • 2. 神奇代码备份恢复工具源码备份

一,序

软件行业流传着一幅漫画:开发软件就像制造小轿车,不是一开始就有设计图,也不是将轮子、车身、车门、发动机按部就班安装上去就可以的,而是大概先出现独轮车,接着出现自行车,然后是滑板车,之后是三轮自行车,继而是两轮摩托车··……

如此反复迭代,最后才得到成型的小轿车。这幅漫画讽刺的是开发新系统时“想当然”的做法,反映的是真实的探索过程。

其实,不仅开发系统是这样,即使是开发一个小工具,也遵循类似的原则,毕竟大部分软件被开发出来,是需要去解决实际中遇到的难题的。

二,逸事

n年前,小C曾入职一家软件公司,公司信息安全管理比较严格,员工办公电脑无法接入任何外部未授权设备。

在工作之中,也曾经做过一些技术预研,积累了一些代码。

公司有一个技术论坛,方便大家技术交流。论坛发帖采用后台审核机制。大家可以把自己遇到的问题以及代码发出来切磋。

对于这样的机制,少量的内容是合适的。一次偶然的机会发现论坛里面的短消息功能可以发送比较长的文本内容。

于是,问题便转变成了:如何把一个项目文件的内容快速的放到一个文本文件里面,并且接受到这个文本内容后如何快速恢复成项目原来的结构。

从软件需求的角度来说,这里面引申出2个基本需求:

  1. 项目文件备份到单个的文本文件
  2. 单个的文本文件恢复成项目文件

三,为什么今天要提这个工具

因为今天,程序员其实是最具有开源精神的一类团体,有太多的内容和Idea需要通过code来交流。

虽然说有github、gitee等代码托管平台,但那是和文章是属于2套系统,而且代码托管平台上的代码是随时变动的。

于是便催生了发表文章的同时,能够方便将示例代码一同提交的需求,而且这个代码还需要反映出项目的结构,这跟上面的2个需求是不谋而合的!

四,操作界面

运行1、2下载 procode-0.0.1.jar

运行3下载 procode-simple-0.0.1.jar

1. 文章发表者备份项目步骤

首先,选择需要备份的项目目录,并选择需要备份的文件类型

点击开始备份按钮,生成bak文件,成功后给出提示

备份成功后,提示是否查看备份文件
选择ok,则打开备份文件所在目录

2. 文章发表者恢复项目操作步骤

切换到代码恢复tab,选择bak文件,以及恢复文件保存目录

点击开始恢复

恢复后的文件如图:

使用开发工具导入如下

3. 文章阅读者恢复项目步骤

参考

五,附件

1. 示例备份文件

上面步骤演示生成的备份文件 spring-config-refresh_10241110.bak,大家可以自行验证文件恢复功能!

//l
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=".0.0"xmlns:xsi=""xsi:schemaLocation=".0.0 .0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.fly</groupId><artifactId>spring-config-refresh</artifactId><version>1.0</version><packaging>jar</packaging><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>&porting.outputEncoding>UTF-8</porting.outputEncoding><java.version>1.8</java.version></properties><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.4.RELEASE</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>commons-configuration</groupId><artifactId>commons-configuration</artifactId><version>1.10</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.6</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><scope>provided</scope></dependency></dependencies><build><finalName>${project.artifactId}-${project.version}</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>
//goto srcmainjavacomflydemoApplication.javapackage com.fly.demo;import java.io.IOException;
import java.URL;import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import io.ClassPathResource;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.util.ResourceUtils;slf4j.Slf4j;/*** * SpringBoot 启动入口* * @author 00fly* @version [版本号, 2018年7月20日]* @see [相关类/方法]* @since [产品/模块版本]*/
@Slf4j
@EnableScheduling
@SpringBootApplication
public class Application implements CommandLineRunner
{public static void main(String[] args){SpringApplication.run(Application.class, args);}@Overridepublic void  args)throws IOException{URL url = ProtectionDomain().getCodeSource().getLocation();log.info("Location URL Path: {}", Path());URL url2 = new ClassPathResource("cron").getURL();log.info("url2 Path: {}", Path());log.info("ResourceUtils.isFileURL: {}", ResourceUtils.isFileURL(url2));log.info("ResourceUtils.isJarURL: {}", ResourceUtils.isJarURL(url2));log.info("ResourceUtils.isJarFileURL: {}", ResourceUtils.isJarFileURL(url2));}
}
//goto srcmainjavacomflydemoautojobShowJob.java
package com.fly.demo.auto.job;import urrent.ScheduledExecutorService;
import urrent.ScheduledThreadPoolExecutor;import org.springframework.beans.factory.annotation.Autowired;
import t.annotation.Bean;
import t.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.urrent.CustomizableThreadFactory;import com.fly.demo.auto.RedisConfig;
import com.fly.demo.auto.ReloadConfig;slf4j.Slf4j;@Slf4j
@Configuration
public class ShowJob
{@AutowiredRedisConfig redisConfig;@AutowiredReloadConfig reloadConfig;/*** 默认线程池的大小为1,配置线程池支持多个定时任务线程并发执行* * @return* @see [类、类#方法、类#成员]*/@BeanScheduledExecutorService scheduledExecutorService(){// wScheduledThreadPool(8);return new ScheduledThreadPoolExecutor(8, new CustomizableThreadFactory("schedule-pool-"));}@Scheduled(cron = "0/5 * 7-23 * * ?")public void run(){fresh(redisConfig);log.info("redisConfig por={}", Port());}
}
//goto srcmainjavacomflydemoautoRedisConfig.java
package com.fly.demo.auto;import org.springframework.beans.factory.annotation.Value;
import t.annotation.Configuration;
import t.annotation.PropertySource;/*** * 配置文件值注入* * @author 00fly* @version [版本号, 2018年7月20日]* @see [相关类/方法]* @since [产品/模块版本]*/
@Configuration
@PropertySource(value = "classpath:redis.properties")
public class RedisConfig
{@Value("${port}")private int port;public int getPort(){return port;}public void setPort(int port){this.port = port;}
}
//goto srcmainjavacomflydemoautoReloadConfig.java
package com.fly.demo.auto;import figuration.ConfigurationException;
import figuration.PropertiesConfiguration;
import loading.FileChangedReloadingStrategy;
import t.annotation.Configuration;slf4j.Slf4j;/*** * 刷新配置文件* * @author 00fly* @version [版本号, 2018年7月20日]* @see [相关类/方法]* @since [产品/模块版本]*/
@Slf4j
@Configuration
public class ReloadConfig
{private PropertiesConfiguration config;public ReloadConfig(){super();try{config = new PropertiesConfiguration("redis.properties");FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();strategy.setRefreshDelay(5000L);// 刷新周期5sconfig.setReloadingStrategy(strategy);}catch (ConfigurationException e){("刷新配置数据出错", e);}}/*** 刷新配置数据到对象中* * @param redisConfig* @see [类、类#方法、类#成员]*/public void refresh(RedisConfig redisConfig){redisConfig.Int("port"));}
}
//goto srcmainjavacomflydemoautotaskMyTask1.java
package com.fly.demo.auto.task;import java.util.Date;import figuration.ConfigurationException;
import figuration.PropertiesConfiguration;
import loading.FileChangedReloadingStrategy;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.fig.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;slf4j.Slf4j;/*** 定时任务类* * @author 00fly* @version [版本号, 2017年4月25日]* @see [相关类/方法]* @since [产品/模块版本]*/
@Slf4j
@Component
public class MyTask1 implements SchedulingConfigurer
{PropertiesConfiguration config;public MyTask1(){super();try{config = new PropertiesConfiguration("demo.properties");FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();strategy.setRefreshDelay(60000L);// 刷新周期1分钟config.setReloadingStrategy(strategy);}catch (ConfigurationException e){(e.getMessage(), e.getCause());}}@Overridepublic void configureTasks(ScheduledTaskRegistrar taskRegistrar){taskRegistrar.addTriggerTask(new Runnable(){@Overridepublic void run(){// 任务逻辑log.info("★★★★★★★ MyTask1 run ★★★★★★★");}}, new Trigger(){@Overridepublic Date nextExecutionTime(TriggerContext triggerContext){// 任务触发,可修改任务的执行周期String cron = String(&#");return new CronTrigger(cron).nextExecutionTime(triggerContext);}});}
}
//goto srcmainjavacomflydemoautotaskMyTask2.java
package com.fly.demo.auto.task;import java.util.Date;
import java.util.ResourceBundle;import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.fig.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;slf4j.Slf4j;/*** 定时任务类* * @author 00fly* @version [版本号, 2017年4月25日]* @see [相关类/方法]* @since [产品/模块版本]*/
@Slf4j
@Component
public class MyTask2 implements SchedulingConfigurer
{ResourceBundle config = Bundle("demo");@Overridepublic void configureTasks(ScheduledTaskRegistrar taskRegistrar){taskRegistrar.addTriggerTask(new Runnable(){@Overridepublic void run(){// 任务逻辑log.info("------ MyTask2 run -------");}}, new Trigger(){@Overridepublic Date nextExecutionTime(TriggerContext triggerContext){// 任务触发,ResourceBundle方式读取修改任务的执行周期ResourceBundle.clearCache();String cron = String(&#");return new CronTrigger(cron).nextExecutionTime(triggerContext);}});}
}
//goto srcmainjavacomflydemoautotaskMyTask3.java
package com.fly.demo.auto.task;import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Date;import org.apachemons.io.IOUtils;
import io.ClassPathResource;
import io.Resource;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.fig.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;slf4j.Slf4j;/*** 定时任务类* * @author 00fly* @version [版本号, 2017年4月25日]* @see [相关类/方法]* @since [产品/模块版本]*/
@Slf4j
@Component
public class MyTask3 implements SchedulingConfigurer
{Resource resource = new ClassPathResource("cron");@Overridepublic void configureTasks(ScheduledTaskRegistrar taskRegistrar){taskRegistrar.addTriggerTask(new Runnable(){@Overridepublic void run(){// 任务逻辑log.info("######### MyTask3 run #########");}}, new Trigger(){@Overridepublic Date nextExecutionTime(TriggerContext triggerContext){String cronText = "*/4 * * * * ?";try{cronText = URL(), StandardCharsets.UTF_8);log.info("resource url = {}, cronText = {}", URL(), cronText);}catch (IOException e){(e.getMessage(), e.getCause());}// 任务触发,IO方式读取可修改任务的执行周期return new CronTrigger(cronText).nextExecutionTime(triggerContext);}});}
}
//goto srcmainresourcesdemo.properties
 = */5 * * * * ?
//goto srcmainresourcesredis.properties
port=8081

2. 神奇代码备份恢复工具源码备份

//l
<project xmlns=".0.0"xmlns:xsi=""xsi:schemaLocation=".0.0 .0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.fly</groupId><artifactId>procode</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>procode</name><url>;/url><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!-- 导入swt --><dependency><groupId&lipse.platform</groupId><artifactId&lipse.swt.win32.win32.x86_64</artifactId><version>3.112.0</version><exclusions><exclusion><groupId&lipse.platform</groupId><artifactId&k.linux.aarch64</artifactId></exclusion><exclusion><groupId&lipse.platform</groupId><artifactId&k.linux.arm</artifactId></exclusion></exclusions></dependency><!-- 导入jface --><dependency><groupId&lipse.platform</groupId><artifactId&lipse.jface</artifactId><version>3.17.0</version><exclusions><exclusion><groupId&lipse.platform</groupId><artifactId&lipse.swt</artifactId></exclusion></exclusions></dependency><dependency><groupId&lipse.platform</groupId><artifactId&lipse.equinoxmon</artifactId><version>3.11.0</version></dependency><dependency><groupId&lipse.platform</groupId><artifactId&ands</artifactId><version>3.9.800</version></dependency></dependencies><build><plugins><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.7.0</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins></build>
</project>
//goto srcmainjavacomflyprocodecoreDateUtil.java
package com.;SimpleDateFormat;public class DateUtil
{public final static String MMDDHHMM = "_MMddHHmm";public static final String getCurrDateTimeStr(String formatStr){SimpleDateFormat sdf = new SimpleDateFormat(formatStr);return sdf.format(System.currentTimeMillis());}public static final String getCurrDateTimeStr(){return getCurrDateTimeStr(MMDDHHMM);}
}
//goto srcmainjavacomflyprocodecoreService.java
package com.;import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;import com.fly.procode.process.RunProgress;/*** 核心业务逻辑类*/
public class Service extends Observable
{// 项目源代码目录private String sourcePath;private RunProgress runProcess;// 构造函数public Service(){super();}public RunProgress getRunProcess(){return runProcess;}public void setRunProcess(RunProgress runProcess){this.runProcess = runProcess;}public String getSourcePath(){return sourcePath;}public void setSourcePath(String sourcePath){this.sourcePath = sourcePath;}// 创建备份文件public void createBakFile(String bootPath, String bakFileName, List<String> fileFilter, String pwdtext){// InputStream,OutputStream 并不负责文件创建或删除// 这部分功能由File来实现File bakfile = new File(bakFileName);if (ists()){bakfile.delete();}if (!"".equals(pwdtext)){// new FileOutputStream(File file,boolean append)try (OutputStream fos = new FileOutputStream(bakFileName, false); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, "ISO-8859-1"))){writer.write("It is a encrypt backup File");wLine();}catch (IOException e){e.printStackTrace();}}// 备份文件bakFile(bootPath, bakFileName, fileFilter, pwdtext);}// 递归备份文件private void bakFile(String bootPath, String bakFileName, List<String> fileFilter, String pwdtext){File file = new File(sourcePath);if (ists() && file.isDirectory() && !Name().startsWith(".")){File[] files = file.listFiles();for (int i = 0; i < files.length; i++){File f1 = files[i];if (f1.isDirectory()){Service p = new Service();p.setRunProcess(runProcess);p.addObserver(runProcess);p.Path());p.bakFile(bootPath, bakFileName, fileFilter, pwdtext);}else if (f1.isFile()){if (f1.getPath().contains("\target\")){return;}if (Name(), fileFilter)){setChanged();notifyObservers("开始处理文件: " + f1.getName());List<String> list = new ArrayList<String>();String text = "//goto " + f1.getPath().substring(bootPath.length());list.add(text);list.addAll(Path()));writeFile(list, bakFileName, pwdtext);}}}}}// 以append 方式将text 写入 bakFileprivate void writeFile(List<String> list, String bakFileName, String pwdtext){// 设置缓冲区大小为8192 bytestry (OutputStream os = new FileOutputStream(bakFileName, true); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "ISO-8859-1"), 8192)){for (String text : list){writer.write(text);wLine();}}catch (IOException e){e.printStackTrace();}}// 获取文件内容private List<String> getFiletext(String filePath){// 设置缓冲区大小为8192 bytesList<String> list = new ArrayList<String>();try (InputStream is = new FileInputStream(filePath); BufferedReader in = new BufferedReader(new InputStreamReader(is, "ISO-8859-1"), 8192)){String text;while ((text = in.readLine()) != null){list.add(text);}return list;}catch (IOException e){e.printStackTrace();return null;}}// 是否为需要备份的文件类型private boolean isExtraFile(String fileName, List<String> fileFilter){for (String postfix : fileFilter){if (dsWith(postfix)){return true;}}return false;}// 从备份文件恢复文件至dirpublic void createSourceFile(String bakFile, String dir){File f = new File(bakFile);String separator = Property("file.separator");int beginIndex = bakFile.lastIndexOf(separator) + 1;int endIndex = bakFile.indexOf("_");String t;if (endIndex > 0){t = bakFile.substring(beginIndex, endIndex) + separator;}else{t = bakFile.substring(beginIndex, bakFile.indexOf(".")) + separator;}dir = dir + t;List<String> list = Path());BufferedWriter writer = null;for (String text : list){try{if (im().startsWith("//goto ")){// close old fileif (writer != null){writer.close();}// creat new fileint pos = text.indexOf("//goto ") + 7;File file = new File(dir + text.substring(pos));ParentFile().mkdirs();OutputStream os = new FileOutputStream(file);writer = new BufferedWriter(new OutputStreamWriter(os, "8859_1"), 8192);}else{if (writer != null){writer.write(text);wLine();}}}catch (IOException e){e.printStackTrace();}}try{if (writer != null){writer.close();}}catch (IOException e){e.printStackTrace();}}
}
//goto srcmainjavacomflyprocodeprocessRunProgress.java
package com.fly.procode.process;import flect.InvocationTargetException;
import java.util.List;
import java.util.Observable;
import java.util.Observer;untime.IProgressMonitor;
lipse.jface.operation.IRunnableWithProgress;import com.Service;/*** * 创建代码进度条线程* * @author 00fly* @version [版本号, 2017年5月3日]* @see [相关类/方法]* @since [产品/模块版本]*/
public class RunProgress implements IRunnableWithProgress, Observer
{private String sourcePath;private String bakFileName;private List<String> fileFilter;private String pwdtext;private IProgressMonitor monitor;public RunProgress(String bootPath, String bakFileName, List<String> fileFilter, String pwdtext){super();this.sourcePath = bootPath;this.bakFileName = bakFileName;this.fileFilter = fileFilter;this.pwdtext = pwdtext;}@Overridepublic void run(IProgressMonitor monitor)throws InvocationTargetException, InterruptedException{// 在当前目录,创建并运行脚本try{monitor.beginTask("代码备份", IProgressMonitor.UNKNOWN);monitor.subTask("代码备份中......");creatAndRun(sourcePath, bakFileName, fileFilter, pwdtext, monitor);monitor.done();}catch (Exception e){throw new Cause(), e.getMessage());}}// 运行代码创建程序private void creatAndRun(String sourcePath, String bakFileName, List<String> fileFilter, String pwdtext, IProgressMonitor monitor){itor = monitor;Service service = new Service();service.setRunProcess(this);service.addObserver(this);service.setSourcePath(sourcePath);ateBakFile(sourcePath, bakFileName, fileFilter, pwdtext);}@Overridepublic void update(Observable observable, Object msg){if (msg instanceof String){monitor.subTask((String)msg);}}
}
//goto srcmainjavacomflyprocodeuiProCodeTool.java
package com.fly.procode.ui;import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.URL;
import java.util.ArrayList;
import java.util.List;import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;import com.DateUtil;
import com.Service;public class ProCodeTool extends JFrame
{private static final long serialVersionUID = 965289280779529771L;// 设置窗口风格的菜单JCheckBoxMenuItem windows = new JCheckBoxMenuItem("Windows", true);JCheckBoxMenuItem metal = new JCheckBoxMenuItem("Metal");// 构造public ProCodeTool(){// 加载图标URL imgURL = Class().getResource("/resource/icon.gif");if (imgURL != null){Image image = DefaultToolkit().createImage(imgURL);setIconImage(image);}setTitle("代码备份恢复工具 V1.1.0");// 设置宽高setSize(540, 340);// 得到当前系统screenSizeDimension screenSize = DefaultToolkit().getScreenSize();// 得到当前frameSizeDimension frameSize = Size();// 自适应处理if (frameSize.height > screenSize.height){frameSize.height = screenSize.height;}if (frameSize.width > screenSize.width){frameSize.width = screenSize.width;}setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);addMenu();add(new TabPanePanel());setWindowStyle("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");setResizable(false);setVisible(true);// 窗口关闭监听器addWindowListener(new WindowAdapter(){@Overridepublic void windowClosing(WindowEvent e){it(0);}});}// Menu setprivate void addMenu(){JMenuBar mb = new JMenuBar();// 菜单1JMenu windowStyle = new JMenu("窗口风格");windows.addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent actionevent){String plaf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";setWindowStyle(plaf);windows.setState(true);metal.setState(false);}});metal.addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent actionevent){String plaf = "javax.al.MetalLookAndFeel";setWindowStyle(plaf);windows.setState(false);metal.setState(true);}});JMenuItem exit = new JMenuItem("退出");exit.addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent arg0){it(0);}});windowStyle.add(windows);windowStyle.add(metal);windowStyle.addSeparator();windowStyle.add(exit);mb.add(windowStyle);// 菜单2JMenu help = new JMenu("帮助");JMenuItem use = new JMenuItem("使用指南");use.addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent arg0){JOptionPane.showMessageDialog(null,"请按以下顺序操作:" + "n 备份" + "n 1. 选择项目源文件的目录。" + "n 2. 选择需要备份的文件类型。" + "n 3. 选择备份文件输出目录。" + "n 4. 导出文件。" + "n " + "n 恢复 " + "n 1. 到备份目录下选择备份文件。" + "n 2. 选择备份文件的恢复目录。" + "n 3. 恢复文件","使用指南",JOptionPane.INFORMATION_MESSAGE);}});JMenuItem about = new JMenuItem("关于工具");about.addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent arg0){JOptionPane.showMessageDialog(null, "代码备份恢复工具,00fly 于2009年9月。", "关于本工具", JOptionPane.INFORMATION_MESSAGE);}});help.add(use);help.addSeparator();help.add(about);mb.add(help);setJMenuBar(mb);}// 根据字串设置窗口外观private void setWindowStyle(String plaf){try{// 设定用户界面的外观UIManager.setLookAndFeel(plaf);// 将用户界面改成当前设定的外观SwingUtilities.updateComponentTreeUI(this);}catch (Exception e){}}// 对话框class TheDialogAdapter extends WindowAdapter{Dialog d;public void init(Dialog d){this.d = d;}@Overridepublic void windowClosing(WindowEvent e){d.setVisible(false);}}// 卡片class TabPanePanel extends JPanel implements ChangeListener{private static final long serialVersionUID = 7226393795721621635L;JTabbedPane jtp;BakPanl p1;RestorePanl p2;TabPanePanel(){jtp = new JTabbedPane();p1 = new BakPanl();p1.setVisible(true);p2 = new RestorePanl();p2.setVisible(true);jtp.addTab(" 代码备份 ", p1);jtp.addTab(" 代码恢复 ", p2);jtp.addChangeListener(this);setLayout(new BorderLayout());add(jtp, BorderLayout.CENTER);}@Overridepublic void stateChanged(ChangeEvent e){if (e.getSource() == jtp){int i = ((Source()).getSelectedIndex();if (i == 0){p1.setVisible(true);p2.setVisible(false);}else{p1.setVisible(false);p2.setVisible(true);}}}}// 备份面板class BakPanl extends JPanel{private static final long serialVersionUID = 4050369107824321460L;// 界面组件JLabel sourceLabel = new JLabel("项目源文件目录:");JTextField tsourcePath = new JTextField(null, 45);JButton sourceBrowse = new JButton("选择");JLabel fileTypeLabel = new JLabel("源文件类型:");JCheckBox jb1 = new JCheckBox(".java", true);JCheckBox jb2 = new JCheckBox(".jsp", true);JCheckBox jb3 = new JCheckBox(".xml", true);JCheckBox jb4 = new JCheckBox(".properties", true);JCheckBox jb5 = new JCheckBox(".classpath");JCheckBox jb6 = new JCheckBox(".project");JCheckBox jb7 = new JCheckBox(".mymetadata");JCheckBox jb8 = new JCheckBox(".sql");JCheckBox jb9 = new JCheckBox(".htm");JCheckBox jb10 = new JCheckBox(".html");JCheckBox jb11 = new JCheckBox(".vm");JLabel pwdLabel = new JLabel("可选项:");JButton pwdButton = new JButton("设置密码");JTextField tpwd = new JTextField(null, 10);JLabel bakeLabel = new JLabel("备份至目录:");JTextField tbakPath = new JTextField(null, 45);JButton bakBrowse = new JButton("选择");JButton bakButton = new JButton("开始备份");public BakPanl(){super();setLayout(null);setBackground(Color.WHITE);// 项目源文件路径sourceLabel.setBounds(20, 30, 120, 18);add(sourceLabel);tsourcePath.setFocusable(false);tsourcePath.setBounds(120, 30, 300, 20);add(tsourcePath);sourceBrowse.setBounds(430, 30, 70, 20);sourceBrowse.addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent arg0){JFileChooser fc = new JFileChooser();fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);if (fc.showOpenDialog(null) == 1){return;}else{// 新源文件目录被选中,清空密码tpwd.setText(null);File f = fc.getSelectedFile();if (f.getPath().endsWith("\")){tsourcePath.AbsolutePath());}else{tsourcePath.AbsolutePath() + "\");}pwdButton.setEnabled(true);}}});add(sourceBrowse);// 文件类型fileTypeLabel.setBounds(20, 60, 120, 18);add(fileTypeLabel);jb1.setBounds(120, 60, 120, 18);jb1.setBackground(Color.WHITE);jb2.setBounds(120, 80, 120, 18);jb2.setBackground(Color.WHITE);jb3.setBounds(120, 100, 120, 18);jb3.setBackground(Color.WHITE);jb4.setBounds(120, 120, 120, 18);jb4.setBackground(Color.WHITE);jb5.setBounds(250, 60, 120, 18);jb5.setBackground(Color.WHITE);jb6.setBounds(250, 80, 120, 18);jb6.setBackground(Color.WHITE);jb7.setBounds(250, 100, 120, 18);jb7.setBackground(Color.WHITE);jb8.setBounds(250, 120, 120, 18);jb8.setBackground(Color.WHITE);jb9.setBounds(380, 60, 120, 18);jb9.setBackground(Color.WHITE);jb10.setBounds(380, 80, 120, 18);jb10.setBackground(Color.WHITE);jb11.setBounds(380, 100, 120, 18);jb11.setBackground(Color.WHITE);add(jb1);add(jb2);add(jb3);add(jb4);add(jb5);add(jb6);add(jb7);add(jb8);add(jb9);add(jb10);add(jb11);// 密码项pwdLabel.setBounds(20, 150, 120, 18);add(pwdLabel);pwdButton.setBounds(120, 150, 100, 20);pwdButton.setEnabled(false);pwdButton.addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent arg0){try{String pwd = JOptionPane.showInputDialog(null, "请输入8位密码(包括字母,数字和字符):", "带密码备份", JOptionPane.INFORMATION_MESSAGE).trim();if (!"".im()) && pwd.length() == 8){// 设置密码tpwd.setText(pwd);JOptionPane.showMessageDialog(null, "密码设置成功!", "提示", JOptionPane.INFORMATION_MESSAGE);}else{// 清空密码tpwd.setText(null);JOptionPane.showMessageDialog(null, "密码设置失败!", "警告", JOptionPane.WARNING_MESSAGE);}}catch (Exception e){// 清空密码tpwd.setText(null);JOptionPane.showMessageDialog(null, "密码设置失败!", "警告", JOptionPane.WARNING_MESSAGE);}}});add(pwdButton);add(tpwd);// 备份目录bakeLabel.setBounds(20, 180, 120, 18);add(bakeLabel);tbakPath.setBounds(120, 180, 300, 20);tbakPath.setFocusable(false);tbakPath.setText(new File(" ").getAbsolutePath().trim());add(tbakPath);bakBrowse.setBounds(430, 180, 70, 20);bakBrowse.addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent arg0){JFileChooser fc = new JFileChooser(new File("").getAbsoluteFile());fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);if (fc.showOpenDialog(null) == 1){return;}else{File f = fc.getSelectedFile();if (f.getPath().endsWith("\")){tbakPath.AbsolutePath());}else{tbakPath.AbsolutePath() + "\");}}}});add(bakBrowse);bakButton.setBounds(220, 220, 100, 30);bakButton.addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent arg0){if (!"".Text())){String sourcePath = Text();String bakFileName = Text() + new File(sourcePath).getName() + CurrDateTimeStr() + ".bak";List<String> list = new ArrayList<String>();if (jb1.isSelected()){list.add(".java");}if (jb2.isSelected()){list.add(".jsp");}if (jb3.isSelected()){list.add(".xml");}if (jb4.isSelected()){list.add(".properties");}if (jb5.isSelected()){list.add(".classpath");}if (jb6.isSelected()){list.add(".project");}if (jb7.isSelected()){list.add(".mymetadata");}if (jb8.isSelected()){list.add(".sql");}if (jb9.isSelected()){list.add(".htm");}if (jb10.isSelected()){list.add(".html");}if (jb11.isSelected()){list.add(".vm");}try{Service service = new Service();String pwdtext = Text();service.setSourcePath(sourcePath);ateBakFile(sourcePath, bakFileName, list, pwdtext);String message = "创建明文备份文件 " + bakFileName + " 成功!";if (!"".equals(pwdtext)){message = "创建加密备份文件 " + bakFileName + " 成功!";}JOptionPane.showMessageDialog(null, message);}catch (RuntimeException e){e.printStackTrace();JOptionPane.showMessageDialog(null, "创建备份文件 " + bakFileName + " 失败!", "错误", JOptionPane.ERROR_MESSAGE);}}else{JOptionPane.showMessageDialog(null, "项目源文件目录不可为空!", "警告", JOptionPane.WARNING_MESSAGE);return;}}});add(bakButton);}}// 恢复面板class RestorePanl extends JPanel{private static final long serialVersionUID = -4960976746526411789L;// 界面组件JLabel bakeLabel = new JLabel("选择备份文件:");JTextField tbakPath = new JTextField(null, 45);JButton bakBrowse = new JButton("选择");JLabel sourceLabel = new JLabel("恢复至目录:");JTextField tsourcePath = new JTextField(null, 45);JButton sourceBrowse = new JButton("选择");JButton resetButton = new JButton("开始恢复");public RestorePanl(){super();setLayout(null);// LabelbakeLabel.setBounds(20, 30, 120, 18);add(bakeLabel);// TexttbakPath.setBounds(120, 30, 300, 20);tbakPath.setFocusable(false);add(tbakPath);// ButtonbakBrowse.setBounds(430, 30, 70, 20);bakBrowse.addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent arg0){JFileChooser fc = new JFileChooser(new File("").getAbsoluteFile());fc.setFileSelectionMode(JFileChooser.FILES_ONLY);if (fc.showOpenDialog(null) == 1){return;}else{File f = fc.getSelectedFile();tbakPath.AbsolutePath());}}});add(bakBrowse);// LabelsourceLabel.setBounds(20, 100, 120, 18);add(sourceLabel);// TexttsourcePath.setBounds(120, 100, 300, 20);tsourcePath.setFocusable(false);tsourcePath.setText(new File(" ").getAbsolutePath().trim());add(tsourcePath);// ButtonsourceBrowse.setBounds(430, 100, 70, 20);sourceBrowse.addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent arg0){JFileChooser fc = new JFileChooser();fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);if (fc.showOpenDialog(null) == 1){return;}else{File f = fc.getSelectedFile();if (f.getPath().endsWith("\")){tsourcePath.AbsolutePath());}else{tsourcePath.AbsolutePath() + "\");}}}});add(sourceBrowse);// 恢复resetButton.setBounds(220, 170, 100, 30);resetButton.addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent arg0){if ("".Text()) || "".Text())){JOptionPane.showMessageDialog(null, "备份文件或恢复目录不可为空!", "警告", JOptionPane.WARNING_MESSAGE);return;}else{String dir = Text();try{new Service().Text(), dir);JOptionPane.showMessageDialog(null, "恢复文件到目录: " + dir + " 成功!");}catch (RuntimeException e){JOptionPane.showMessageDialog(null, "恢复文件失败,请重新检查备份文件参数!", "错误", JOptionPane.ERROR_MESSAGE);}}}});add(resetButton);}}public static void main(String[] args){SwingUtilities.invokeLater(() -> new ProCodeTool());}
}
//goto srcmainjavacomflyprocodeuiProCodeToolSimple.java
package com.fly.procode.ui;import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;/*** * 工具UI简化版本* * @author 00fly* @version [版本号, 2023年3月3日]* @see [相关类/方法]* @since [产品/模块版本]*/
public class ProCodeToolSimple extends JFrame
{private static final long serialVersionUID = -2145267777297657212L;JFrame frame = this;public ProCodeToolSimple(){initComponent();this.setVisible(true);this.setResizable(true);this.setAlwaysOnTop(true);this.setTitle("代码恢复工具 V1.0");this.setBounds(400, 200, 1200, 550);this.setDefaultCloseOperation(EXIT_ON_CLOSE);try{// 设定用户界面的外观UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");SwingUtilities.updateComponentTreeUI(this);}catch (Exception e){}}/*** 组件初始化* * @see [类、类#方法、类#成员]*/private void initComponent(){// 加载图标this.setIconImage(getToolkit().createImage(getClass().getResource("/image/icon.gif")));JTextArea textArea = new JTextArea();textArea.setToolTipText("请输入全部待恢复代码");// JTextArea不自带滚动条,因此就需要把文本区放到一个滚动窗格中JScrollPane scroll = new JScrollPane(textArea);scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);JButton button = new JButton("开始恢复代码");this.add(scroll, BorderLayout.CENTER);this.add(button, BorderLayout.SOUTH);button.addMouseListener(new MouseAdapter(){@Overridepublic void mouseClicked(MouseEvent event){try{String dir = new File(" ").getCanonicalPath();Text(), dir);JOptionPane.showMessageDialog(frame, "恢复文件到目录: " + dir + " 成功!", "提示", JOptionPane.INFORMATION_MESSAGE);}catch (Exception e){JOptionPane.showMessageDialog(frame, e.getMessage(), "系統异常", JOptionPane.ERROR_MESSAGE);}}});}/*** 恢复文件至dir* * @param text* @param dir*/private void createSourceFile(String text, String dir){String[] textArr = text.split("n");BufferedWriter writer = null;for (String line : textArr){try{if (im().startsWith("//goto ")){// close old fileif (writer != null){writer.close();}// creat new fileint pos = line.indexOf("//goto ") + 7;File file = new File(dir + line.substring(pos));ParentFile().mkdirs();OutputStream os = new FileOutputStream(file);writer = new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8), 8192);}else{if (writer != null){writer.write(line);wLine();}}}catch (IOException e){e.printStackTrace();}}try{if (writer != null){writer.close();}}catch (IOException e){e.printStackTrace();}}public static void main(String[] args){SwingUtilities.invokeLater(() -> new ProCodeToolSimple());}
}
//goto srcmainjavacomflyprocodeuiProCodeToolSJ.java
package com.fly.procode.ui;import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import flect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;lipse.jface.dialogs.IInputValidator;
lipse.jface.dialogs.InputDialog;
lipse.jface.dialogs.MessageDialog;
lipse.jface.dialogs.ProgressMonitorDialog;
lipse.jface.operation.IRunnableWithProgress;
lipse.jface.window.Window;
lipse.swt.SWT;
lipse.swt.events.SelectionAdapter;
lipse.swt.events.SelectionEvent;
aphics.Image;
aphics.Rectangle;
lipse.swt.widgets.Button;
lipse.swt.widgets.Composite;
lipse.swt.widgets.DirectoryDialog;
lipse.swt.widgets.Display;
lipse.swt.widgets.FileDialog;
lipse.swt.widgets.Label;
lipse.swt.widgets.Menu;
lipse.swt.widgets.MenuItem;
lipse.swt.widgets.Shell;
lipse.swt.widgets.TabFolder;
lipse.swt.widgets.TabItem;
lipse.swt.widgets.Text;import com.DateUtil;
import com.Service;
import com.fly.procode.process.RunProgress;/*** * swt jface版本* * @author 00fly* @version [版本号, 2020年4月24日]* @see [相关类/方法]* @since [产品/模块版本]*/
public class ProCodeToolSJ
{Display display;Shell lipse.swt.widgets.List lipse.swt.widgets.List listPage;public ProCodeToolSJ(){super();display = new Display();shell = new Shell(display, SWT.MIN | SWT.CLOSE | SWT.ON_TOP);InputStream is = Class().getResourceAsStream("/image/icon.gif");if (is != null){shell.setImage(new Image(display, is));}shell.setText("代码备份恢复工具 V1.1.0");shell.setSize(540, 383);Rectangle screeRec = Bounds();Rectangle shellRec = Bounds();if (shellRec.height > screeRec.height){shellRec.height = screeRec.height;}if (shellRec.width > screeRec.width){shellRec.width = screeRec.width;}shell.setLocation((screeRec.width - shellRec.width) / 2, (screeRec.height - shellRec.height) / 2);addMenu();setContent();shell.open();while (!shell.isDisposed()){if (!adAndDispatch()){display.sleep();}}display.dispose();}// mainpublic static void main(String[] args){new ProCodeToolSJ();}// Menu setprivate void addMenu(){Menu m = new Menu(shell, SWT.BAR);// create a file menu and add an exit itemMenuItem file = new MenuItem(m, SWT.CASCADE);file.setText("文件(&F)");file.setAccelerator(SWT.CTRL + 'F');Menu filemenu = new Menu(shell, SWT.DROP_DOWN);file.setMenu(filemenu);MenuItem openMenuItem = new MenuItem(filemenu, SWT.CASCADE);openMenuItem.setText("最近备份(&O)");openMenuItem.setAccelerator(SWT.CTRL + 'O');Menu submenu = new Menu(shell, SWT.DROP_DOWN);openMenuItem.setMenu(submenu);MenuItem childItem = new MenuItem(submenu, SWT.PUSH);childItem.setText("Child");MenuItem saveMenuItem = new MenuItem(filemenu, SWT.CASCADE);saveMenuItem.setText("最近恢复(&S)");saveMenuItem.setAccelerator(SWT.CTRL + 'S');Menu submenu2 = new Menu(shell, SWT.DROP_DOWN);saveMenuItem.setMenu(submenu2);MenuItem childItem2 = new MenuItem(submenu2, SWT.PUSH);childItem2.setText("Child2");new MenuItem(filemenu, SWT.SEPARATOR);MenuItem exitMenuItem = new MenuItem(filemenu, SWT.PUSH);exitMenuItem.setText("退出(&X)");exitMenuItem.setAccelerator(SWT.CTRL + 'X');exitMenuItem.addSelectionListener(new SelectionAdapter(){@Overridepublic void widgetSelected(SelectionEvent e){it(0);}});// create a Help menu and add an about itemMenuItem help = new MenuItem(m, SWT.CASCADE);help.setText("帮助(&H)");help.setAccelerator(SWT.CTRL + 'H');Menu helpmenu = new Menu(shell, SWT.DROP_DOWN);help.setMenu(helpmenu);MenuItem useMenuItem = new MenuItem(helpmenu, SWT.PUSH);useMenuItem.setText("使用指南(&U)");new MenuItem(helpmenu, SWT.SEPARATOR);MenuItem aboutMenuItem = new MenuItem(helpmenu, SWT.PUSH);aboutMenuItem.setText("关于工具(&A)");useMenuItem.addSelectionListener(new SelectionAdapter(){@Overridepublic void widgetSelected(SelectionEvent event){MessageDialog.openInformation(shell,"使用指南","请按以下顺序操作:" + "n 文件备份" + "n 1. 选择项目源文件的目录。" + "n 2. 选择需要备份的文件类型。" + "n 3. 选择备份文件输出目录。" + "n 4. 导出文件。" + "n " + "n 文件恢复 " + "n 1. 到备份目录下选择备份文件。" + "n 2. 选择备份文件的恢复目录。" + "n 3. 恢复文件");}});aboutMenuItem.addSelectionListener(new SelectionAdapter(){@Overridepublic void widgetSelected(SelectionEvent event){MessageDialog.openInformation(shell, "关于本工具", "代码备份恢复工具。nn00fly 于2010年5月。");}});shell.setMenuBar(m);}public void setContent(){TabFolder tabFolder = new TabFolder(shell, SWT.NONE);tabFolder.setBounds(5, 2, 525, 328);TabItem item = new TabItem(tabFolder, SWT.NONE);item.setText(" 代码备份 ");item.setControl(new BakPanlTab(tabFolder, SWT.NONE));TabItem item2 = new TabItem(tabFolder, SWT.NONE);item2.setText(" 代码恢复 ");item2.setControl(new RestorePanlTab(tabFolder, SWT.NONE));}// 备份面板class BakPanlTab extends Composite{Button pwdButton;Text tpwd = new Text(this, SWT.NONE);public BakPanlTab(Composite c, int style){super(c, style);Label sourceLabel = new Label(this, SWT.NONE);sourceLabel.setText("项目源文件目录:");sourceLabel.setBounds(20, 10, 100, 18);final Text tsourcePath = new Text(this, SWT.BORDER);tsourcePath.SystemColor(SWT.COLOR_WHITE));tsourcePath.setEditable(false);tsourcePath.setBounds(120, 10, 300, 20);Button sourceBrowse = new Button(this, SWT.PUSH);sourceBrowse.setText("选择");sourceBrowse.setBounds(430, 10, 70, 20);sourceBrowse.addSelectionListener(new SelectionAdapter(){@Overridepublic void widgetSelected(SelectionEvent event){DirectoryDialog dialog = new DirectoryDialog(shell);dialog.Text());String fileName = dialog.open();if (fileName != null){if (dsWith("\")){tsourcePath.setText(fileName);}else{tsourcePath.setText(fileName + "\");}// 新源文件目录被选中,清空密码tpwd.setText("");pwdButton.setEnabled(true);}}});Label fileTypeLabel = new Label(this, SWT.NONE);fileTypeLabel.setText("源文件类型:");fileTypeLabel.setBounds(20, 40, 100, 18);listJava = lipse.swt.widgets.List(this, SWT.BORDER | SWT.V_SCROLL | SWT.SIMPLE | SWT.MULTI);listJava.setBounds(120, 40, 120, 128);listJava.setToolTipText("选择需要备份的源文件类型,支持多选!");String[] fileTypes = {".java", ".xml", ".yml", ".yaml", ".properties", ".sql", ".md"};listJava.setItems(fileTypes);IntStream.range(0, fileTypes.length).forEach(index -> listJava.select(index));listPage = lipse.swt.widgets.List(this, SWT.BORDER | SWT.V_SCROLL | SWT.SIMPLE | SWT.MULTI);listPage.setBounds(280, 40, 120, 128);listPage.setToolTipText("选择需要备份的源文件类型,支持多选!");String[] fileTypes2 = {"Dockerfile", ".htm", ".html", ".js", ".css", ".vue", ".jsp", ".sh"};listPage.setItems(fileTypes2);IntStream.range(0, fileTypes2.length).forEach(index -> listPage.select(index));Label pwdLabel = new Label(this, SWT.NONE);pwdLabel.setText("可选项:");pwdLabel.setBounds(20, 180, 100, 18);pwdButton = new Button(this, SWT.PUSH);pwdButton.setText("设置密码");pwdButton.setBounds(120, 180, 100, 20);pwdButton.setEnabled(false);pwdButton.addSelectionListener(new SelectionAdapter(){@Overridepublic void widgetSelected(SelectionEvent event){InputDialog dlg = new InputDialog(shell, "带密码备份", "请输入8位密码(包括字母,数字和字符):", "", new LengthValidator());if (dlg.open() == Window.OK){tpwd.Value());MessageDialog.openInformation(shell, "恭喜你", "密码设置成功!");}}});Label bakeLabel = new Label(this, SWT.NONE);bakeLabel.setText("备份至目录:");bakeLabel.setBounds(20, 210, 100, 18);final Text tbakPath = new Text(this, SWT.BORDER);tbakPath.SystemColor(SWT.COLOR_WHITE));tbakPath.setEditable(false);tbakPath.setText(new File(" ").getAbsolutePath().trim());tbakPath.setBounds(120, 210, 300, 20);Button bakBrowse = new Button(this, SWT.PUSH);bakBrowse.setText("选择");bakBrowse.setBounds(430, 210, 70, 20);bakBrowse.addSelectionListener(new SelectionAdapter(){@Overridepublic void widgetSelected(SelectionEvent event){DirectoryDialog dialog = new DirectoryDialog(shell);dialog.Text());String fileName = dialog.open();if (fileName != null){if (dsWith("\")){tbakPath.setText(fileName);}else{tbakPath.setText(fileName + "\");}}}});Button bakButton = new Button(this, SWT.PUSH);bakButton.setText("开始备份");bakButton.setBounds(220, 250, 100, 40);bakButton.addSelectionListener(new SelectionAdapter(){@Overridepublic void widgetSelected(SelectionEvent event){if ("".Text())){MessageDialog.openWarning(shell, "警告", "项目源文件目录不可为空!");return;}if (SelectionCount() + SelectionCount() < 1){MessageDialog.openError(shell, "警告", "请选择源文件类型,支持多选!");listJava.setFocus();return;}String sourcePath = Text();String bakFileName = Text() + new File(sourcePath).getName() + CurrDateTimeStr() + ".bak";try{List<String> fileTypes = new ArrayList<String>();for (String it : Selection()){fileTypes.add(it);}for (String it : Selection()){fileTypes.add(it);}String pwdtext = Text();IRunnableWithProgress runnable = new RunProgress(sourcePath, bakFileName, fileTypes, pwdtext);new ProgressMonitorDialog(shell).run(true, false, runnable);String message = "创建明文备份文件 " + bakFileName + " 成功!";if (!"".equals(pwdtext)){message = "创建加密备份文件 " + bakFileName + " 成功!";}MessageDialog.openInformation(shell, "提示", message);}catch (InvocationTargetException e){MessageDialog.openError(shell, "警告", e.getMessage());}catch (InterruptedException e){MessageDialog.openInformation(shell, "Cancelled", "刷新操作被用户取消!");}catch (RuntimeException e){MessageDialog.openError(shell, "错误", "创建备份文件 " + bakFileName + " 失败!");}if (MessageDialog.openConfirm(shell, "查看备份文件", "处理完成,是否直接查看文件?")){try{Desktop().open(new File(bakFileName).getParentFile());}catch (IOException e){}}}});}// 密码长度验证class LengthValidator implements IInputValidator{@Overridepublic String isValid(String newText){int len = newText.length();if (len < 8){return "长度少于8位";}if (len > 8){return "长度大于8位";}return null;}}}// 恢复面板class RestorePanlTab extends Composite{public RestorePanlTab(Composite c, int style){super(c, style);Label bakeLabel = new Label(this, SWT.NONE);bakeLabel.setText("选择备份文件:");bakeLabel.setBounds(20, 30, 100, 18);final Text tbakPath = new Text(this, SWT.BORDER);tbakPath.SystemColor(SWT.COLOR_WHITE));tbakPath.setEditable(false);tbakPath.setBounds(120, 30, 300, 20);Button bakBrowse = new Button(this, SWT.PUSH);bakBrowse.setText("选择");bakBrowse.setBounds(430, 30, 70, 20);bakBrowse.addSelectionListener(new SelectionAdapter(){@Overridepublic void widgetSelected(SelectionEvent event){FileDialog fileDialog = new FileDialog(shell, SWT.OPEN);fileDialog.setText("选择文件");String[] filterExt = {"*.bak"};fileDialog.setFilterExtensions(filterExt);String selected = fileDialog.open();if (selected == null){return;}tbakPath.setText(selected);}});Label sourceLabel = new Label(this, SWT.NONE);sourceLabel.setText("恢复至目录:");sourceLabel.setBounds(20, 100, 100, 18);final Text tsourcePath = new Text(this, SWT.BORDER);tsourcePath.SystemColor(SWT.COLOR_WHITE));tsourcePath.setEditable(false);tsourcePath.setText(new File(" ").getAbsolutePath().trim());tsourcePath.setBounds(120, 100, 300, 20);Button sourceBrowse = new Button(this, SWT.PUSH);sourceBrowse.setText("选择");sourceBrowse.setBounds(430, 100, 70, 20);sourceBrowse.addSelectionListener(new SelectionAdapter(){@Overridepublic void widgetSelected(SelectionEvent event){DirectoryDialog dialog = new DirectoryDialog(shell);dialog.Text());String fileName = dialog.open();if (fileName != null){if (dsWith("\")){tsourcePath.setText(fileName);}else{tsourcePath.setText(fileName + "\");}}}});Button resetButton = new Button(this, SWT.PUSH);resetButton.setText("开始恢复");resetButton.setBounds(220, 170, 100, 40);resetButton.addSelectionListener(new SelectionAdapter(){@Overridepublic void widgetSelected(SelectionEvent event){if ("".Text()) || "".Text())){MessageDialog.openWarning(shell, "警告", "备份文件或恢复目录不可为空!");}else{String dir = Text();try{new Service().Text(), dir);MessageDialog.openInformation(shell, "提示", "恢复文件到目录: " + dir + " 成功!");}catch (RuntimeException e){MessageDialog.openError(shell, "错误", "恢复文件失败,请重新检查备份文件!");}}}});}}
}

本文发布于:2024-02-01 05:28:33,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170673651534224.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:逸事   操作指南   备份   神奇   代码
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23