Search in sources :

Example 1 with ProgressListener

use of org.netxms.client.ProgressListener in project netxms by netxms.

the class PackageManager method installPackage.

/**
 * Install new package
 */
private void installPackage() {
    FileDialog fd = new FileDialog(getSite().getShell(), SWT.OPEN);
    fd.setText(Messages.get().PackageManager_SelectFile);
    String npiName = fd.open();
    if (npiName != null) {
        try {
            final File npiFile = new File(npiName);
            final PackageInfo p = new PackageInfo(npiFile);
            final NXCSession session = (NXCSession) ConsoleSharedData.getSession();
            new ConsoleJob(Messages.get().PackageManager_InstallPackage, this, Activator.PLUGIN_ID, null) {

                @Override
                protected void runInternal(final IProgressMonitor monitor) throws Exception {
                    final long id = session.installPackage(p, new File(npiFile.getParent(), p.getFileName()), new ProgressListener() {

                        long prevAmount = 0;

                        @Override
                        public void setTotalWorkAmount(long amount) {
                            monitor.beginTask(Messages.get(getDisplay()).PackageManager_UploadPackage, (int) amount);
                        }

                        @Override
                        public void markProgress(long amount) {
                            monitor.worked((int) (amount - prevAmount));
                            prevAmount = amount;
                        }
                    });
                    p.setId(id);
                    runInUIThread(new Runnable() {

                        @Override
                        public void run() {
                            packageList.add(p);
                            viewer.setInput(packageList.toArray());
                        }
                    });
                }

                @Override
                protected String getErrorMessage() {
                    return Messages.get().PackageManager_InstallError;
                }
            }.start();
        } catch (IOException e) {
            MessageDialogHelper.openError(getSite().getShell(), Messages.get().PackageManager_Error, Messages.get().PackageManager_PkgFileOpenError + e.getLocalizedMessage());
        }
    }
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) NXCSession(org.netxms.client.NXCSession) ProgressListener(org.netxms.client.ProgressListener) PackageInfo(org.netxms.client.packages.PackageInfo) ConsoleJob(org.netxms.ui.eclipse.jobs.ConsoleJob) IOException(java.io.IOException) FileDialog(org.eclipse.swt.widgets.FileDialog) File(java.io.File) PartInitException(org.eclipse.ui.PartInitException) IOException(java.io.IOException) NXCException(org.netxms.client.NXCException)

Example 2 with ProgressListener

use of org.netxms.client.ProgressListener in project netxms by netxms.

the class AgentFileManager method downloadFile.

/**
 * Downloads file
 * @throws NXCException
 * @throws IOException
 */
private void downloadFile(final AgentFile sf, final String localName, final IProgressMonitor monitor, final boolean subTask, ConsoleJobCallingServerJob job) throws IOException, NXCException {
    if (subTask)
        monitor.subTask(String.format("Downloading file %s", sf.getFullName()));
    final AgentFileData file = session.downloadFileFromAgent(objectId, sf.getFullName(), 0, false, new ProgressListener() {

        @Override
        public void setTotalWorkAmount(long workTotal) {
            if (!subTask)
                monitor.beginTask(String.format("Downloading file %s", sf.getFullName()), (int) workTotal);
        }

        @Override
        public void markProgress(long workDone) {
            monitor.worked((int) workDone);
        }
    }, job);
    if (file.getFile() != null) {
        File outputFile = new File(localName);
        outputFile.createNewFile();
        InputStream in = new FileInputStream(file.getFile());
        OutputStream out = new FileOutputStream(outputFile);
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
        outputFile.setLastModified(sf.getModifyicationTime().getTime());
    }
}
Also used : ProgressListener(org.netxms.client.ProgressListener) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) AgentFile(org.netxms.client.server.AgentFile) File(java.io.File) AgentFileData(org.netxms.client.AgentFileData) FileInputStream(java.io.FileInputStream)

Example 3 with ProgressListener

use of org.netxms.client.ProgressListener in project netxms by netxms.

the class ImageLibrary method editImage.

/**
 * @param galleryItem
 * @param name
 * @param category
 * @param fileName
 */
protected void editImage(final GalleryItem galleryItem, final String name, final String category, final String fileName) {
    final LibraryImage image = (LibraryImage) galleryItem.getData();
    new ConsoleJob(Messages.get().ImageLibrary_UpdateJob, this, Activator.PLUGIN_ID, null) {

        @Override
        protected void runInternal(final IProgressMonitor monitor) throws Exception {
            if (fileName != null) {
                verifyImageFile(fileName);
                FileInputStream stream = null;
                try {
                    final long fileSize = new File(fileName).length();
                    stream = new FileInputStream(fileName);
                    byte[] imageData = new byte[(int) fileSize];
                    stream.read(imageData);
                    image.setBinaryData(imageData);
                } finally {
                    if (stream != null)
                        stream.close();
                }
            }
            if (!image.isProtected()) {
                image.setName(name);
                image.setCategory(category);
            }
            session.modifyImage(image, new ProgressListener() {

                private long prevDone = 0;

                @Override
                public void setTotalWorkAmount(long workTotal) {
                    monitor.beginTask(Messages.get(getDisplay()).ImageLibrary_UpdateImage, (int) workTotal);
                }

                @Override
                public void markProgress(long workDone) {
                    monitor.worked((int) (workDone - prevDone));
                    prevDone = workDone;
                }
            });
            ImageProvider.getInstance(display).syncMetaData();
            refreshImages();
            /* TODO: update single element */
            monitor.done();
        }

        @Override
        protected String getErrorMessage() {
            return Messages.get().ImageLibrary_UpdateError;
        }
    }.start();
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ProgressListener(org.netxms.client.ProgressListener) LibraryImage(org.netxms.client.LibraryImage) ConsoleJob(org.netxms.ui.eclipse.jobs.ConsoleJob) File(java.io.File) SWTException(org.eclipse.swt.SWTException) IOException(java.io.IOException) NXCException(org.netxms.client.NXCException) FileInputStream(java.io.FileInputStream)

Example 4 with ProgressListener

use of org.netxms.client.ProgressListener in project netxms by netxms.

the class ImageLibrary method uploadNewImage.

/**
 * @param name
 * @param category
 * @param fileName
 */
protected void uploadNewImage(final String name, final String category, final String fileName) {
    new ConsoleJob(Messages.get().ImageLibrary_UploadJob, this, Activator.PLUGIN_ID, null) {

        @Override
        protected void runInternal(final IProgressMonitor monitor) throws Exception {
            verifyImageFile(fileName);
            final LibraryImage image = new LibraryImage();
            final long fileSize = new File(fileName).length();
            FileInputStream stream = null;
            try {
                stream = new FileInputStream(fileName);
                byte[] imageData = new byte[(int) fileSize];
                stream.read(imageData);
                image.setBinaryData(imageData);
                image.setName(name);
                image.setCategory(category);
            } finally {
                if (stream != null)
                    stream.close();
            }
            session.createImage(image, new ProgressListener() {

                private long prevDone = 0;

                @Override
                public void setTotalWorkAmount(long workTotal) {
                    monitor.beginTask(Messages.get(getDisplay()).ImageLibrary_UploadImage, (int) workTotal);
                }

                @Override
                public void markProgress(long workDone) {
                    monitor.worked((int) (workDone - prevDone));
                    prevDone = workDone;
                }
            });
            // TODO: check
            ImageProvider.getInstance(display).syncMetaData();
            refreshImages();
            /* TODO: update local copy */
            monitor.done();
        }

        @Override
        protected String getErrorMessage() {
            return Messages.get().ImageLibrary_UploadError;
        }
    }.start();
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ProgressListener(org.netxms.client.ProgressListener) ConsoleJob(org.netxms.ui.eclipse.jobs.ConsoleJob) LibraryImage(org.netxms.client.LibraryImage) File(java.io.File) SWTException(org.eclipse.swt.SWTException) IOException(java.io.IOException) NXCException(org.netxms.client.NXCException) FileInputStream(java.io.FileInputStream)

Example 5 with ProgressListener

use of org.netxms.client.ProgressListener in project netxms by netxms.

the class ObjectToolExecutor method executeFileDownload.

/**
 * @param node
 * @param tool
 * @param inputValues
 * @param inputValues
 */
private static void executeFileDownload(final ObjectContext node, final ObjectTool tool, final Map<String, String> inputValues) {
    final NXCSession session = (NXCSession) ConsoleSharedData.getSession();
    // $NON-NLS-1$
    String[] parameters = tool.getData().split("\u007F");
    final String fileName = parameters[0];
    final int maxFileSize = Integer.parseInt(parameters[1]);
    // $NON-NLS-1$
    final boolean follow = parameters[2].equals("true") ? true : false;
    ConsoleJobCallingServerJob job = new ConsoleJobCallingServerJob(Messages.get().ObjectToolsDynamicMenu_DownloadFromAgent, null, Activator.PLUGIN_ID, null) {

        @Override
        protected String getErrorMessage() {
            return String.format(Messages.get().ObjectToolsDynamicMenu_DownloadError, fileName, node.object.getObjectName());
        }

        @Override
        protected void runInternal(final IProgressMonitor monitor) throws Exception {
            final AgentFileData file = session.downloadFileFromAgent(node.object.getObjectId(), fileName, maxFileSize, follow, inputValues, node.getAlarmId(), new ProgressListener() {

                @Override
                public void setTotalWorkAmount(long workTotal) {
                    monitor.beginTask("Download file " + fileName, (int) workTotal);
                }

                @Override
                public void markProgress(long workDone) {
                    monitor.worked((int) workDone);
                }
            }, this);
            runInUIThread(new Runnable() {

                @Override
                public void run() {
                    try {
                        // $NON-NLS-1$ //$NON-NLS-2$
                        String secondaryId = Long.toString(node.object.getObjectId()) + "&" + URLEncoder.encode(fileName, "UTF-8");
                        AgentFileViewer.createView(secondaryId, node.object.getObjectId(), file, follow);
                    } catch (Exception e) {
                        final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
                        MessageDialogHelper.openError(window.getShell(), Messages.get().ObjectToolsDynamicMenu_Error, String.format(Messages.get().ObjectToolsDynamicMenu_ErrorOpeningView, e.getLocalizedMessage()));
                    }
                }
            });
        }
    };
    job.start();
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) NXCSession(org.netxms.client.NXCSession) AgentFileData(org.netxms.client.AgentFileData) PartInitException(org.eclipse.ui.PartInitException) ConsoleJobCallingServerJob(org.netxms.ui.eclipse.jobs.ConsoleJobCallingServerJob) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ProgressListener(org.netxms.client.ProgressListener)

Aggregations

ProgressListener (org.netxms.client.ProgressListener)11 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)9 File (java.io.File)6 AgentFileData (org.netxms.client.AgentFileData)6 ConsoleJob (org.netxms.ui.eclipse.jobs.ConsoleJob)5 FileInputStream (java.io.FileInputStream)4 IOException (java.io.IOException)4 PartInitException (org.eclipse.ui.PartInitException)4 NXCException (org.netxms.client.NXCException)4 NXCSession (org.netxms.client.NXCSession)4 ConsoleJobCallingServerJob (org.netxms.ui.eclipse.jobs.ConsoleJobCallingServerJob)4 AgentFile (org.netxms.client.server.AgentFile)3 SWTException (org.eclipse.swt.SWTException)2 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)2 LibraryImage (org.netxms.client.LibraryImage)2 StartClientToServerFileUploadDialog (org.netxms.ui.eclipse.filemanager.dialogs.StartClientToServerFileUploadDialog)2 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 List (java.util.List)1