将任务栏加载栏添加到jframe

时间:2015-09-09 14:57:48

标签: java swt taskbar

我找到了将taskbaricon作为加载栏的方法,但我想将它添加到我拥有的现有应用程序中。 这是我编辑过的例子:

package main;

import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;

public class Main {
    static Display display;
    static Shell shell;
    static TaskItem getTaskBarItem () {
        TaskBar bar = display.getSystemTaskBar();
        if (bar == null) return null;
        TaskItem item = bar.getItem(shell);
        if (item == null) item = bar.getItem(null);
        return item;
    }
    public static void main(String[] args) {
        display = new Display();
        shell = new Shell(display);
        TaskItem item = getTaskBarItem();
        item.setProgressState(SWT.NORMAL);
        item.setProgress(20);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();
    }
}

这将创建一个窗口并将任务栏中的加载栏设置为20%。 这是我的下载脚本,添加的内容在一个框中:

package client;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.BufferedOutputStream;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLConnection;
import java.net.URL;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TaskBar;
import org.eclipse.swt.widgets.TaskItem;

public class CacheDownloader {

    public client client;
    private final int BUFFER = 1024;
    File directory = new File(Signlink.findcachedir());
    -------------------------
    |static Display display;|
    |static Shell shell;    |
    -------------------------
    private String fileToExtract = getCacheDir() + getArchivedName();

    public CacheDownloader(client client) {
        this.client = client;
    }

    private String getCacheDir() {
        return Signlink.findcachedir();
    }

    public void drawLoadingText(String text) {
        client.drawLoadingText(35, text);
    }

    public void drawLoadingText(int amount, String text) {
        client.drawLoadingText(amount, text);
    }

    private String getCacheLink() {
        return Configuration.CACHE_DOWNLOAD_URL;
    }

    private int getCacheVersion() {
        return Configuration.VERSION;
    }

    private void deleteZIP(String fileName){
        // A File object to represent the filename
        File f = new File(getCacheDir() + fileName);

        // Make sure the file or directory exists and isn't write protected
        if (!f.exists())
            throw new IllegalArgumentException(
                    "Delete: no such file or directory: " + fileName);

        if (!f.canWrite())
            throw new IllegalArgumentException("Delete: write protected: "
                    + fileName);

        // If it is a directory, make sure it is empty
        if (f.isDirectory()) {
            String[] files = f.list();
            if (files.length > 0)
                throw new IllegalArgumentException(
                        "Delete: directory not empty: " + fileName);
        }

        // Attempt to delete it
        boolean success = f.delete();

        if (!success)
            throw new IllegalArgumentException("Delete: deletion failed");

    }

    public static void delete(File file)

            throws IOException{

            if(file.isDirectory()){

                //directory is empty, then delete it
                if(file.list().length==0){

                   file.delete();
                   System.out.println("Directory is deleted : " 
                                                     + file.getAbsolutePath());

                }else{

                   //list all the directory contents
                   String files[] = file.list();

                   for (String temp : files) {
                      //construct the file structure
                      File fileDelete = new File(file, temp);

                      //recursive delete
                     delete(fileDelete);
                   }

                   //check the directory again, if empty then delete it
                   if(file.list().length==0){
                     file.delete();
                     System.out.println("Directory is deleted : " 
                                                      + file.getAbsolutePath());
                   }
                }

            }else{
                //if file, then delete it
                file.delete();
                System.out.println("File is deleted : " + file.getAbsolutePath());
            }
        }

    public CacheDownloader downloadCache() {
        try {
            File location = new File(getCacheDir());
            File version = new File(getCacheDir() + "/cacheVersion" + getCacheVersion() + ".dat");

            if(!location.exists() || (!version.exists())) {

                if (location.exists()) 
                    delete(directory);

                downloadFile(getCacheLink(), getArchivedName());

                BufferedWriter versionFile = new BufferedWriter(new FileWriter(getCacheDir() + "/cacheVersion" + getCacheVersion() + ".dat"));
                versionFile.close();

                deleteZIP(getArchivedName());
                System.out.println("Done deleting cache.");
            } else {
                    return null;
                }
        } catch(Exception e) {

        }
        return null;
    }
    ------------------------------------------------
    |static TaskItem getTaskBarItem () {           |
    |   TaskBar bar = display.getSystemTaskBar();  |
    |   if (bar == null) return null;              |
    |   TaskItem item = bar.getItem(shell);        |
    |   if (item == null) item = bar.getItem(null);|
    |   return item;                               |
    |}                                             |
    ------------------------------------------------
    private void downloadFile(String adress, String localFileName) {
        OutputStream out = null;
        URLConnection conn;
        InputStream in = null;

        try {

            URL url = new URL(adress);
            out = new BufferedOutputStream(
                    new FileOutputStream(getCacheDir() + "/" +localFileName)); 

            conn = url.openConnection();
            in = conn.getInputStream(); 

            byte[] data = new byte[BUFFER]; 

            int numRead;
            long numWritten = 0;
            int length = conn.getContentLength();


            while((numRead = in.read(data)) != -1) {
                out.write(data, 0, numRead);
                numWritten += numRead;

                int percentage = (int)(((double)numWritten / (double)length) * 100D);
                drawLoadingText(percentage, "Downloading Cache - " + percentage + "%");
                System.out.println("Downloaded:" + percentage + "%");
                ------------------------------------
                |TaskItem item = getTaskBarItem(); |
                |item.setProgressState(SWT.NORMAL);|
                |item.setProgress(percentage);     |
                ------------------------------------
            }

            System.out.println(localFileName + "\t" + numWritten);
            drawLoadingText(100, "Download Finished!");

            unZip();
            System.out.println("Done unzipping.");

        } catch (Exception exception) {
            exception.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException ioe) {
            }
        }

    }

    private String getArchivedName() {
        String name = getCacheLink().replace("%20", " ");
        int lastSlashIndex = getCacheLink().lastIndexOf('/');
        if (lastSlashIndex >= 0 
                && lastSlashIndex < getCacheLink().length() -1) { 
            return name.substring(lastSlashIndex + 1);
        } else {
            System.err.println("Error Downloading Game Files.");
        }
        return "";
    }




    private void unZip() {

        try {
            InputStream in = 
                    new BufferedInputStream(new FileInputStream(fileToExtract));
            ZipInputStream zin = new ZipInputStream(in);
            ZipEntry e;

            while((e=zin.getNextEntry()) != null) {

                if(e.isDirectory()) {
                    (new File(getCacheDir() + e.getName())).mkdir();
                } else {

                    if (e.getName().equals(fileToExtract)) {
                        unzip(zin, fileToExtract);
                        break;
                    }
                    unzip(zin, getCacheDir() + e.getName());
                }
                System.out.println("unzipping: " + e.getName());
            }
            zin.close();

        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    private void unzip(ZipInputStream zin, String s) 
            throws IOException {

        FileOutputStream out = new FileOutputStream(s);
        byte [] b = new byte[BUFFER];
        int len = 0;

        while ((len = zin.read(b)) != -1) {
            out.write(b,0,len);
        }
        out.close();
    }
}

我的应用程序已经有了一个jframe。 这就是我希望它在任务栏上的样子: This is what I want it to look like, but without the map icon.

0 个答案:

没有答案