예제 #1
0
 private void startTaskAction() {
   try {
     _task.execute();
   } catch (TaskException e) {
     sout(e.getMessage());
   }
 }
예제 #2
0
  /** create a test task and wire it up with a task handler that dumps output to the textarea */
  @SuppressWarnings("unchecked")
  private void _setupTask() {

    TaskExecutorIF<ByteBuffer> functor =
        new TaskExecutorAdapter<ByteBuffer>() {
          public ByteBuffer doInBackground(Future<ByteBuffer> swingWorker, SwingUIHookAdapter hook)
              throws Exception {

            _initHook(hook);

            // set the license key
            MapLookup.setLicenseKey(ttfLicense.getText());
            // get the uri for the static map
            String uri =
                MapLookup.getMap(
                    Double.parseDouble(ttfLongi.getText()),
                    Double.parseDouble(ttfLati.getText()),
                    Integer.parseInt(ttfSizeW.getText()),
                    Integer.parseInt(ttfSizeH.getText()),
                    Integer.parseInt(ttfZoom.getText()));
            sout("Google Maps URI=" + uri);

            // get the map from Google
            GetMethod get = new GetMethod(uri);
            new HttpClient().executeMethod(get);

            ByteBuffer data = HttpUtils.getMonitoredResponse(hook, get);

            try {
              _img = ImageUtils.toCompatibleImage(ImageIO.read(data.getInputStream()));
              sout("converted downloaded data to image...");
            } catch (Exception e) {
              _img = null;
              sout("The URI is not an image. Data is downloaded, can't display it as an image.");
              _respStr = new String(data.getBytes());
            }

            return data;
          }

          @Override
          public String getName() {
            return _task.getName();
          }
        };

    _task =
        new SimpleTask(
            new TaskManager(),
            functor,
            "HTTP GET Task",
            "Download an image from a URL",
            AutoShutdownSignals.Daemon);

    _task.addStatusListener(
        new PropertyChangeListener() {
          public void propertyChange(PropertyChangeEvent evt) {
            sout(":: task status change - " + ProgressMonitorUtils.parseStatusMessageFrom(evt));
            lblProgressStatus.setText(ProgressMonitorUtils.parseStatusMessageFrom(evt));
          }
        });

    _task.setTaskHandler(
        new SimpleTaskHandler<ByteBuffer>() {
          @Override
          public void beforeStart(AbstractTask task) {
            sout(":: taskHandler - beforeStart");
          }

          @Override
          public void started(AbstractTask task) {
            sout(":: taskHandler - started ");
          }
          /** {@link SampleApp#_initHook} adds the task status listener, which is removed here */
          @Override
          public void stopped(long time, AbstractTask task) {
            sout(":: taskHandler [" + task.getName() + "]- stopped");
            sout(":: time = " + time / 1000f + "sec");
            task.getUIHook().clearAllStatusListeners();
          }

          @Override
          public void interrupted(Throwable e, AbstractTask task) {
            sout(":: taskHandler [" + task.getName() + "]- interrupted - " + e.toString());
          }

          @Override
          public void ok(ByteBuffer value, long time, AbstractTask task) {
            sout(
                ":: taskHandler ["
                    + task.getName()
                    + "]- ok - size="
                    + (value == null ? "null" : value.toString()));
            if (_img != null) {
              sout("********************************");
              sout("            INSTRUCTIONS");
              sout("********************************");
              sout("Click anywhere on the loaded");
              sout("map and it will save the");
              sout("coordinates and open up a new");
              sout("window with those coordinates.");
              sout("");
              sout("The clicked coordinates are");
              sout("saved under a combo box in");
              sout("this window under get quit.");
              sout("********************************");

              _displayImgInFrame();
            } else _displayRespStrInFrame();
          }

          @Override
          public void error(Throwable e, long time, AbstractTask task) {
            sout(":: taskHandler [" + task.getName() + "]- error - " + e.toString());
          }

          @Override
          public void cancelled(long time, AbstractTask task) {
            sout(" :: taskHandler [" + task.getName() + "]- cancelled");
          }
        });
  }
예제 #3
0
 private void quitProgram() {
   _task.shutdown();
   System.exit(0);
 }