神奇的代码恢复工具

news2024/11/15 23:48:50

文章目录

  • 概述
  • 工具展示
  • 工具下载地址
  • 运行过程
  • 附件待恢复代码

概述

小C是一名程序猿,他有好多新奇的点子,也乐于把这些变成文字分享给大家。这些分享大部分都与代码相关,在文章里面把这些代码全部按本来的结构展示出来也不是一件容易的事!

工具展示

这不,最近开发了一个小工具,界面介绍如下:在这里插入图片描述

工具下载地址

procode-simple-0.0.1.jar

运行过程

在输入框里面输入待恢复的代码,点击"开始恢复代码" 便生成原来代码结构的代码。
在这里插入图片描述
大家可以下载jar,拷贝附件代码尝试运行!

附件待恢复代码

代码恢复数据框输入的内容为:

//goto pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.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>http://maven.apache.org</url>
	<properties>
		<java.version>1.8</java.version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	<dependencies>
		<!-- 导入swt -->
		<dependency>
			<groupId>org.eclipse.platform</groupId>
			<artifactId>org.eclipse.swt.win32.win32.x86_64</artifactId>
			<version>3.112.0</version>
			<exclusions>
				<exclusion>
					<groupId>org.eclipse.platform</groupId>
					<artifactId>org.eclipse.swt.gtk.linux.aarch64</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.eclipse.platform</groupId>
					<artifactId>org.eclipse.swt.gtk.linux.arm</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<!-- 导入jface -->
		<dependency>
			<groupId>org.eclipse.platform</groupId>
			<artifactId>org.eclipse.jface</artifactId>
			<version>3.17.0</version>
			<exclusions>
				<exclusion>
					<groupId>org.eclipse.platform</groupId>
					<artifactId>org.eclipse.swt</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.eclipse.platform</groupId>
			<artifactId>org.eclipse.equinox.common</artifactId>
			<version>3.11.0</version>
		</dependency>
		<dependency>
			<groupId>org.eclipse.platform</groupId>
			<artifactId>org.eclipse.core.commands</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 src\main\java\com\fly\procode\core\DateUtil.java
package com.fly.procode.core;

import java.text.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 src\main\java\com\fly\procode\core\Service.java
package com.fly.procode.core;

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 (bakfile.exists())
        {
            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");
                writer.newLine();
            }
            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 (file.exists() && file.isDirectory() && !file.getName().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.setSourcePath(f1.getPath());
                    p.bakFile(bootPath, bakFileName, fileFilter, pwdtext);
                }
                else if (f1.isFile())
                {
                    if (isExtraFile(f1.getName(), fileFilter))
                    {
                        setChanged();
                        notifyObservers("开始处理文件: " + f1.getName());
                        List<String> list = new ArrayList<String>();
                        String text = "//goto " + f1.getPath().substring(bootPath.length());
                        list.add(text);
                        list.addAll(getFiletext(f1.getPath()));
                        writeFile(list, bakFileName, pwdtext);
                    }
                }
            }
        }
    }
    
    // 以append 方式将text 写入 bakFile
    private void writeFile(List<String> list, String bakFileName, String pwdtext)
    {
        // 设置缓冲区大小为8192 bytes
        try (OutputStream os = new FileOutputStream(bakFileName, true); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "ISO-8859-1"), 8192))
        {
            for (String text : list)
            {
                writer.write(text);
                writer.newLine();
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    
    // 获取文件内容
    private List<String> getFiletext(String filePath)
    {
        // 设置缓冲区大小为8192 bytes
        List<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 (fileName.endsWith(postfix))
            {
                return true;
            }
        }
        return false;
    }
    
    // 从备份文件恢复文件至dir
    public void createSourceFile(String bakFile, String dir)
    {
        File f = new File(bakFile);
        String separator = System.getProperty("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 = getFiletext(f.getPath());
        BufferedWriter writer = null;
        for (String text : list)
        {
            try
            {
                if (text.trim().startsWith("//goto "))
                {
                    // close old file
                    if (writer != null)
                    {
                        writer.close();
                    }
                    // creat new file
                    int pos = text.indexOf("//goto ") + 7;
                    File file = new File(dir + text.substring(pos));
                    file.getParentFile().mkdirs();
                    OutputStream os = new FileOutputStream(file);
                    writer = new BufferedWriter(new OutputStreamWriter(os, "8859_1"), 8192);
                }
                else
                {
                    if (writer != null)
                    {
                        writer.write(text);
                        writer.newLine();
                    }
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        try
        {
            if (writer != null)
            {
                writer.close();
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}
//goto src\main\java\com\fly\procode\process\RunProgress.java
package com.fly.procode.process;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;

import com.fly.procode.core.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;
    }
    
    @Override
    public 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 InvocationTargetException(e.getCause(), e.getMessage());
        }
    }
    
    // 运行代码创建程序
    private void creatAndRun(String sourcePath, String bakFileName, List<String> fileFilter, String pwdtext, IProgressMonitor monitor)
    {
        this.monitor = monitor;
        Service service = new Service();
        service.setRunProcess(this);
        service.addObserver(this);
        service.setSourcePath(sourcePath);
        service.createBakFile(sourcePath, bakFileName, fileFilter, pwdtext);
    }
    
    @Override
    public void update(Observable observable, Object msg)
    {
        if (msg instanceof String)
        {
            monitor.subTask((String)msg);
        }
    }
}
//goto src\main\java\com\fly\procode\ui\ProCodeToolFrame.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 ProCodeToolFrame extends JFrame
{
    private static final long serialVersionUID = -2145267777297657212L;
    
    JFrame frame = this;
    
    public ProCodeToolFrame()
    {
        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()
        {
            @Override
            public void mouseClicked(MouseEvent event)
            {
                try
                {
                    String dir = new File(" ").getCanonicalPath();
                    createSourceFile(textArea.getText(), 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 (line.trim().startsWith("//goto "))
                {
                    // close old file
                    if (writer != null)
                    {
                        writer.close();
                    }
                    // creat new file
                    int pos = line.indexOf("//goto ") + 7;
                    File file = new File(dir + line.substring(pos));
                    file.getParentFile().mkdirs();
                    OutputStream os = new FileOutputStream(file);
                    writer = new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8), 8192);
                }
                else
                {
                    if (writer != null)
                    {
                        writer.write(line);
                        writer.newLine();
                    }
                }
            }
            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 ProCodeToolFrame());
    }
}
//goto src\main\java\com\fly\procode\ui\ProCodeToolSJ.java
package com.fly.procode.ui;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;

import com.fly.procode.core.DateUtil;
import com.fly.procode.core.Service;
import com.fly.procode.process.RunProgress;

/**
 * 
 * swt jface版本
 * 
 * @author 00fly
 * @version [版本号, 2020年4月24日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class ProCodeToolSJ
{
    Display display;
    
    Shell shell;
    
    org.eclipse.swt.widgets.List listJava;
    
    org.eclipse.swt.widgets.List listPage;
    
    public ProCodeToolSJ()
    {
        super();
        display = new Display();
        shell = new Shell(display, SWT.MIN | SWT.CLOSE | SWT.ON_TOP);
        InputStream is = this.getClass().getResourceAsStream("/image/icon.gif");
        if (is != null)
        {
            shell.setImage(new Image(display, is));
        }
        shell.setText("代码备份恢复工具 V1.1.0");
        shell.setSize(540, 383);
        Rectangle screeRec = display.getBounds();
        Rectangle shellRec = shell.getBounds();
        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 (!display.readAndDispatch())
            {
                display.sleep();
            }
        }
        display.dispose();
    }
    
    // main
    public static void main(String[] args)
    {
        new ProCodeToolSJ();
    }
    
    // Menu set
    private void addMenu()
    {
        Menu m = new Menu(shell, SWT.BAR);
        // create a file menu and add an exit item
        MenuItem 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()
        {
            @Override
            public void widgetSelected(SelectionEvent e)
            {
                System.exit(0);
            }
        });
        // create a Help menu and add an about item
        MenuItem 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()
        {
            @Override
            public 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()
        {
            @Override
            public void widgetSelected(SelectionEvent event)
            {
                MessageDialog.openInformation(shell, "关于本工具", "代码备份恢复工具。\n\nkailin 于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, 30, 100, 18);
            final Text tsourcePath = new Text(this, SWT.BORDER);
            tsourcePath.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
            tsourcePath.setEditable(false);
            tsourcePath.setBounds(120, 30, 300, 20);
            Button sourceBrowse = new Button(this, SWT.PUSH);
            sourceBrowse.setText("选择");
            sourceBrowse.setBounds(430, 30, 70, 20);
            sourceBrowse.addSelectionListener(new SelectionAdapter()
            {
                @Override
                public void widgetSelected(SelectionEvent event)
                {
                    DirectoryDialog dialog = new DirectoryDialog(shell);
                    dialog.setFilterPath(tsourcePath.getText());
                    String fileName = dialog.open();
                    if (fileName != null)
                    {
                        if (fileName.endsWith("\\"))
                        {
                            tsourcePath.setText(fileName);
                        }
                        else
                        {
                            tsourcePath.setText(fileName + "\\");
                        }
                        // 新源文件目录被选中,清空密码
                        tpwd.setText("");
                        pwdButton.setEnabled(true);
                    }
                }
            });
            Label fileTypeLabel = new Label(this, SWT.NONE);
            fileTypeLabel.setText("源文件类型:");
            fileTypeLabel.setBounds(20, 60, 100, 18);
            
            listJava = new org.eclipse.swt.widgets.List(this, SWT.BORDER | SWT.V_SCROLL | SWT.SIMPLE | SWT.MULTI);
            listJava.setBounds(120, 60, 120, 110);
            listJava.setToolTipText("选择需要备份的源文件类型,支持多选!");
            String[] fileTypes = {".java", ".xml", ".yml", ".properties", ".sql"};
            listJava.setItems(fileTypes);
            
            listPage = new org.eclipse.swt.widgets.List(this, SWT.BORDER | SWT.V_SCROLL | SWT.SIMPLE | SWT.MULTI);
            listPage.setBounds(280, 60, 120, 110);
            listPage.setToolTipText("选择需要备份的源文件类型,支持多选!");
            String[] fileTypes2 = {".html", ".js", ".css", ".vue", ".jsp"};
            listPage.setItems(fileTypes2);
            
            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()
            {
                @Override
                public void widgetSelected(SelectionEvent event)
                {
                    InputDialog dlg = new InputDialog(shell, "带密码备份", "请输入8位密码(包括字母,数字和字符):", "", new LengthValidator());
                    if (dlg.open() == Window.OK)
                    {
                        tpwd.setText(dlg.getValue());
                        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.setBackground(display.getSystemColor(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()
            {
                @Override
                public void widgetSelected(SelectionEvent event)
                {
                    DirectoryDialog dialog = new DirectoryDialog(shell);
                    dialog.setFilterPath(tbakPath.getText());
                    String fileName = dialog.open();
                    if (fileName != null)
                    {
                        if (fileName.endsWith("\\"))
                        {
                            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()
            {
                @Override
                public void widgetSelected(SelectionEvent event)
                {
                    if ("".equals(tsourcePath.getText()))
                    {
                        MessageDialog.openWarning(shell, "警告", "项目源文件目录不可为空!");
                        return;
                    }
                    if (listJava.getSelectionCount() + listPage.getSelectionCount() < 1)
                    {
                        MessageDialog.openError(shell, "警告", "请选择源文件类型,支持多选!");
                        listJava.setFocus();
                        return;
                    }
                    String sourcePath = tsourcePath.getText();
                    String bakFileName = tbakPath.getText() + new File(sourcePath).getName() + DateUtil.getCurrDateTimeStr() + ".bak";
                    try
                    {
                        List<String> fileTypes = new ArrayList<String>();
                        for (String it : listJava.getSelection())
                        {
                            fileTypes.add(it);
                        }
                        for (String it : listPage.getSelection())
                        {
                            fileTypes.add(it);
                        }
                        String pwdtext = tpwd.getText();
                        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.getDesktop().open(new File(bakFileName).getParentFile());
                        }
                        catch (IOException e)
                        {
                        }
                    }
                }
            });
        }
        
        // 密码长度验证
        class LengthValidator implements IInputValidator
        {
            @Override
            public 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.setBackground(display.getSystemColor(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()
            {
                @Override
                public 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.setBackground(display.getSystemColor(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()
            {
                @Override
                public void widgetSelected(SelectionEvent event)
                {
                    DirectoryDialog dialog = new DirectoryDialog(shell);
                    dialog.setFilterPath(tsourcePath.getText());
                    String fileName = dialog.open();
                    if (fileName != null)
                    {
                        if (fileName.endsWith("\\"))
                        {
                            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()
            {
                @Override
                public void widgetSelected(SelectionEvent event)
                {
                    if ("".equals(tbakPath.getText()) || "".equals(tsourcePath.getText()))
                    {
                        MessageDialog.openWarning(shell, "警告", "备份文件或恢复目录不可为空!");
                    }
                    else
                    {
                        String dir = tsourcePath.getText();
                        try
                        {
                            new Service().createSourceFile(tbakPath.getText(), dir);
                            MessageDialog.openInformation(shell, "提示", "恢复文件到目录: " + dir + " 成功!");
                        }
                        catch (RuntimeException e)
                        {
                            MessageDialog.openError(shell, "错误", "恢复文件失败,请重新检查备份文件!");
                        }
                    }
                }
            });
        }
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1084903.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

LMI FocalSpec 3D线共焦传感器 使用笔记1

一.硬件介绍 以上特别注意: 屏蔽线必须接地,因为在现场实际调试中,使用软件调试发现经常 弹窗 传感器丢失警告!! 以上 Position LED 的灯被钣金挡住,无法查看异常现象,能否将指示灯设置在软件界面上? 需要确认是软触发还是硬触发,理论上 硬触发比软触发速度要快.(我们目前使用…

Zilliz X Dify.AI ,快速打造知识库 AI 应用

Zilliz 大模型生态矩阵再迎新伙伴&#xff01;近日&#xff0c;Zilliz 和 Dify.AI 达成合作&#xff0c;Zilliz 旗下的产品 Zilliz Cloud、Milvus 与开源 LLMOps 平台 Dify 社区版进行了深度集成。 01.Zilliz Cloud v.s. Dify Dify 作为开源的 LLMs App 技术栈&#xff0c;在此…

[GXYCTF2019]BabyUpload - 文件上传+绕过(后缀文件类型文件内容.htaccess)

[GXYCTF2019]BabyUpload 解题流程 解题流程 1、上传一句话&#xff0c;提示“后缀不允许ph” 2、修改后缀为jpg&#xff0c;提示“上传类型也太露骨了吧&#xff01;” 3、修改类型为image/jpeg&#xff0c;提示“诶&#xff0c;别蒙我啊&#xff0c;这标志明显还是php啊” 4、…

【Axure教程】将figma导入Axure

Figma和Axure是两个不同的界面设计工具&#xff0c;Figma主要用于创建和协作设计图形界面&#xff08;UI&#xff09;&#xff0c;允许多个设计师和利益相关者同时在云端协作设计项目&#xff1b;Axure是原型设计工具&#xff0c;专注于创建高保真、可交互的原型。大家可以根据…

Java学习笔记(一)——概述

目录 一、Java概述 &#xff08;一&#xff09;Java技术体系平台 &#xff08;二&#xff09;Java重要特点 &#xff08;三&#xff09;Java运行机制及运行过程 &#xff08;四&#xff09;JDK &#xff08;五&#xff09;JRE 二、Java的快速入门 &#xff08;一&#…

掌握Python机器学习:空间模拟与时间预测的实战指南

了解全文点击:《掌握Python机器学习&#xff1a;空间模拟与时间预测的实战指南》 文章目录 一、机器学习原理与概述二、Python编译工具组合安装教程三、掌握Python语法及常见科学计算方法四、机器学习数据清洗五、机器学习与深度学习方法六、机器学习空间模拟实践操作七、机器…

6数据层相关框架-基本

MyBatis常见面试问题&#xff0c;以及和hibernate 的区别等_mybatis和hiberbate区别面试_my_styles的博客-CSDN博客*1、什么是MyBatis&#xff1f;*答&#xff1a;MyBatis是一个可以自定义SQL、存储过程和高级映射的持久层框架。*2、讲下MyBatis的缓存*答&#xff1a;MyBatis的…

实现即时沟通与协作的全功能IM即时通讯系统

在当今竞争激烈的商业环境中&#xff0c;高效的沟通和协作成为企业取得成功的关键。在过去&#xff0c;电子邮件和电话等传统工具是企业之间进行沟通和协作的重要手段&#xff0c;然而&#xff0c;随着科技的发展和社交化的趋势&#xff0c;IM即时通讯系统正逐渐成为企业协作的…

虹科方案 | 虹科ATTO 4K/8K以太网解决方案

一、方案背景 以太网为中小型媒体制作工作室提供经济高效的共享存储解决方案。尽管 10GbE 继续在 4K 工作流程中发挥重要作用&#xff0c;但 8K 等新格式需要额外的带宽。 为了使您的环境适应未来的新制作格式&#xff0c;需要一种更强大、低延迟的连接技术&#xff0c;一种足…

外卖点餐小程序源码 扫码点餐小程序源码

外卖点餐小程序源码 扫码点餐小程序源码 吃饭点外卖&#xff0c;坐车靠窗边&#xff0c;睡觉侧着身&#xff0c;洗澡要放歌&#xff0c;随时随地要自拍.......这些俨然早已成为我们当代新青年的真实生活写照。 近年来外卖行业蓬勃发展&#xff0c;外卖小哥走街串巷&#xff0…

FastAPI学习-26 并发 async / await

前言 有关路径操作函数的 async def 语法以及异步代码、并发和并行的一些背景知识 async 和 await 关键字 如果你正在使用第三方库&#xff0c;它们会告诉你使用 await 关键字来调用它们&#xff0c;就像这样&#xff1a; results await some_library()然后&#xff0c;通…

竞赛 深度学习 机器视觉 人脸识别系统 - opencv python

文章目录 0 前言1 机器学习-人脸识别过程人脸检测人脸对其人脸特征向量化人脸识别 2 深度学习-人脸识别过程人脸检测人脸识别Metric Larning 3 最后 0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 &#x1f6a9; 深度学习 机器视觉 人脸识别系统 该项目…

JVM第三讲:JVM 基础-字节码的增强技术详解

JVM 基础-字节码的增强技术详解 本文是JVM第三讲&#xff0c;JVM 基础-字节码的增强技术。在上文中&#xff0c;着重介绍了字节码的结构&#xff0c;这为我们了解字节码增强技术的实现打下了基础。字节码增强技术就是一类对现有字节码进行修改或者动态生成全新字节码文件的技术…

CV计算机视觉每日开源代码Paper with code速览-2023.10.12

精华置顶 墙裂推荐&#xff01;小白如何1个月系统学习CV核心知识&#xff1a;链接 点击CV计算机视觉&#xff0c;关注更多CV干货 论文已打包&#xff0c;点击进入—>下载界面 点击加入—>CV计算机视觉交流群 1.【目标检测】A Novel Voronoi-based Convolutional Neura…

二叉树题目:二叉树寻路

文章目录 题目标题和出处难度题目描述要求示例数据范围 解法思路和算法代码复杂度分析 题目 标题和出处 标题&#xff1a;二叉树寻路 出处&#xff1a;1104. 二叉树寻路 难度 5 级 题目描述 要求 在一个无限的二叉树上&#xff0c;每个结点都有两个子结点&#xff0c;结…

logicFlow 流程图编辑工具使用及开源地址

一、工具介绍 LogicFlow 是一款流程图编辑框架&#xff0c;提供了一系列流程图交互、编辑所必需的功能和灵活的节点自定义、插件等拓展机制。LogicFlow 支持前端研发自定义开发各种逻辑编排场景&#xff0c;如流程图、ER 图、BPMN 流程等。在工作审批配置、机器人逻辑编排、无…

玩转Linux Shell Terminal Tmux

一、Shell编程☘️ 1. Shell指令快捷操作 1. echo # 系统指令 $ echo $(pwd) # 对于系统自带的pwd&#xff0c;此处不能写echo $pwd# 自定义变量 $ foo$(pwd) $ echo $foo # 不同于pwd&#xff0c;对于自定义的foo&#xff0c;不能用$(foo)2. !! # 假设你先执行了以下原本…

JOSEF约瑟 矿用一般型选择性漏电继电器 LXY2-660 Φ45 JKY1-660

系列型号&#xff1a; JY82A检漏继电器 JY82B检漏继电器 JY82-380/660检漏继电器 JY82-IV检漏继电器 JY82-2P检漏继电器 JY82-2/3检漏继电器 JJKY检漏继电器 JD型检漏继电器 JY82-IV;JY82J JY82-II;JY82-III JY82-1P;JY82-2PA;JY82-2PB JJB-380;JJB-380/660 JD-12…

Generics/泛型, ViewBuilder/视图构造器 的使用

1. Generics 泛型的定义及使用 1.1 创建使用泛型的实例 GenericsBootcamp.swift import SwiftUIstruct StringModel {let info: String?func removeInfo() -> StringModel{StringModel(info: nil)} }struct BoolModel {let info: Bool?func removeInfo() -> BoolModel…

解析Moonbeam的安全性、互操作性和市场竞争力

Moonbeam依托Polkadot Substrate框架构建&#xff0c;用Rust程序设计语言创建的智能合约区块链平台&#xff0c;在继承Polkadot安全性的基础上为项目提供以太坊虚拟机&#xff08;EVM&#xff09;的兼容性和原生的跨链互操作性优势。Moonbeam的EVM兼容性表示开发者无需学习Subs…