Package org.geoforge.guillcogcecl.thread

Source Code of org.geoforge.guillcogcecl.thread.GfrPgsThrIdsBldShtTlosGeometrySpnsAbs

/*
*  Copyright (C) 2011-2014 GeoForge Project
*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU Lesser General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU Lesser General Public License for more details.
*
*  You should have received a copy of the GNU Lesser General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.geoforge.guillcogcecl.thread;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JLabel;
import org.geoforge.guillc.dialog.DlgAbs;
import org.geoforge.guillc.optionpane.GfrOptionPaneAbs;
import org.geoforge.guillc.progressbar.GfrProgressBar;
import org.geoforge.guillc.thread.GfrPgsThrIdsBldShtTlosGeometryAbs;
import org.geoforge.io.header.GfrIoSheetColIdxXlosNamGeometry;
import org.geoforge.io.saver.GfrObjectNamGeometry;
import org.geoforge.io.writer.GfrIoBldSheetGeometryObjs;
import org.geoforge.lang.handler.IGfrHandlerEventListenerThreadJobProgress;
import org.geoforge.lang.util.GfrResBundleLang;
import org.geoforge.java.util.logging.filehandler.FileHandlerLogger;
import org.geoforge.java.lang.string.GfrUtlString;
import org.geoforge.mdldatecl.GfrMdlDatSetTlosEclSpn;
import org.geoforge.wrpbasprssynecl.GfrWrpBasTopSynEclSpns;

/**
*
* @author Amadeus.Sowerby
*
* email: Amadeus.Sowerby_AT_gmail.com
* ... please remove "_AT_" from the above string to get the right email address
*/
abstract public class GfrPgsThrIdsBldShtTlosGeometrySpnsAbs extends GfrPgsThrIdsBldShtTlosGeometryAbs
{
   // ----
   // begin: instantiate logger for this class

   final private static Logger _LOGGER_ = Logger.getLogger(GfrPgsThrIdsBldShtTlosGeometrySpnsAbs.class.getName());

   static
   {
      GfrPgsThrIdsBldShtTlosGeometrySpnsAbs._LOGGER_.addHandler(FileHandlerLogger.s_getInstance());
   }
   // end: instantiate logger for this class
   // ----

   protected GfrPgsThrIdsBldShtTlosGeometrySpnsAbs(
           IGfrHandlerEventListenerThreadJobProgress lst,
           GfrProgressBar pbrItems,
           JLabel lblItems,
           String strPathAbsSourceOri,
           GfrIoSheetColIdxXlosNamGeometry choicesUser) throws Exception
   {
      super(lst,
              pbrItems,
              lblItems,
              strPathAbsSourceOri,
              choicesUser);
   }

   // MEMO: invoked later while interrupted by user
   @Override
   protected void _actionCancelled()
   {
      while (!super._blnDoneJob)
      {
         try
         {
            Thread.sleep(500);
         }
         catch (Exception exc)
         {
            // TODO: workaround? user should manually remove elements!
            System.err.println(exc.getMessage());
            return;
         }
      }

      if (super._setCreatedIds == null)
         return;

      if (super._setCreatedIds.size() < 1)
         return;

      for (String strIdCur : super._setCreatedIds)
      {
         try
         {
            if (GfrWrpBasTopSynEclSpns.getInstance().containsTlo(strIdCur))
               GfrMdlDatSetTlosEclSpn.getInstance().deleteObjectWithId(strIdCur);
         }
         catch (Exception exc)
         {
            // don't care
         }
      }
   }

   private void _doJob_(ArrayList<GfrObjectNamGeometry> lst) throws Exception
   {
      if (lst.size() < 1)
      {
         String str = GfrResBundleLang.s_getInstance().getValue("sentence.noValidDataFoundInList");
         str += "\n\n" + GfrResBundleLang.s_getInstance().getValue("sentence.aborting");

         throw new Exception(str);
      }


      // TODO: set determinate progressBar, send completed amount plus change message: saving

      int intCountItems = 0;
      int intTotalItems = lst.size();

      boolean blnShowProgressItems = false;

      if (intTotalItems > 1)
      {
         blnShowProgressItems = true;
      }

      if (blnShowProgressItems)
         super._pbrItems.setIndeterminate(false);



      String[] strsLabel = new String[lst.size()];
      Point2D.Double[][] p2dss = new Point2D.Double[lst.size()][];
      String[] strsDescription = new String[lst.size()];
      String[] strsUrl = new String[lst.size()];


      String[] strsNamesExisting = GfrWrpBasTopSynEclSpns.getInstance().getSortedNamesTlo();

      for (int i = 0; i < lst.size(); i++)
      {
         GfrObjectNamGeometry objPntIdCur = lst.get(i);

         if (super._blnCanceled)
         {
            super._blnDoneJob = true;
            return;
         }

         // ---

         if (blnShowProgressItems)
         {
            int intPercentItems = intCountItems * 100 / intTotalItems / 3;
            super._lblItems.setText("Processing pointsets, " + intPercentItems + "% done");
            super._pbrItems.setValue(intPercentItems);
            intCountItems++;
         }
         else
         {
            super._lblItems.setText("Processing pointset, please wait ...");
         }

         // ---




         strsUrl[i] = "";
         strsDescription[i] = "";


         String strNameCur = objPntIdCur.getNameUnique();

         String strNameCurCandidate = strNameCur;

         String[] strsNamesExistingAndCurs = new String[strsNamesExisting.length + i];

         for (int j = 0; j < strsNamesExisting.length; j++)
         {
            strsNamesExistingAndCurs[j] = strsNamesExisting[j];
         }

         for (int j = 0; j < i; j++)
         {
            strsNamesExistingAndCurs[strsNamesExisting.length + j] = strsLabel[j];
         }

         if (GfrUtlString.s_contains(strsNamesExistingAndCurs, strNameCur))
            strNameCurCandidate = GfrUtlString.s_createUniqueNameByNumber(strsNamesExistingAndCurs, strNameCur);


         strsLabel[i] = strNameCurCandidate;

         ArrayList<Point2D.Double> altP2d = objPntIdCur.getGeometry();

         Point2D.Double p2ds[] = new Point2D.Double[altP2d.size()];

         for (int j = 0; j < altP2d.size(); j++)
         {
            p2ds[j] = altP2d.get(j);
         }

         p2dss[i] = p2ds;


         // end


      }


      String[] strsId =
              GfrMdlDatSetTlosEclSpn.getInstance().newObjects(strsLabel, strsDescription, strsUrl, p2dss);

      for (int i = 0; i < strsId.length; i++)
      {
         super._setCreatedIds.add(strsId[i]);
      }




      super._blnDoneJob = true;
   }

   @Override
   protected void _doJob()
   {
      JDialog dlgOwner = DlgAbs.s_getDialogOwner(super._pbrItems);

      try
      {
         GfrIoBldSheetGeometryObjs bld = new GfrIoBldSheetGeometryObjs(
                 dlgOwner,
                 _alt,
                 "pointset",
                 (GfrIoSheetColIdxXlosNamGeometry) super._choicesUser);

         bld.doJob();

         _alt.clear();
         _alt = null;

         ArrayList<GfrObjectNamGeometry> lst = bld.getResult();

         if (lst == null)
         {
            // aborted by user!
            // TODO
            System.out.println("Cancelled by user");
            return;
         }

         this._doJob_(lst);


         lst.clear();
         lst = null;
      }
      catch (Exception exc)
      {
         exc.printStackTrace();
         String str = exc.getMessage();

         if (str == null)
            str = "Uncaught exception";

         GfrPgsThrIdsBldShtTlosGeometrySpnsAbs._LOGGER_.severe(str);
         super._strError = str;
         super._blnDoneJob = true;

         super._pbrItems.setIndeterminate(false);

         GfrOptionPaneAbs.s_showDialogWarning(
                 dlgOwner, str);

         try
         {
            _fireEvent();
         }
         catch (Exception exc2)
         {
            exc2.printStackTrace();
         }

         return;
      }
   }

}
TOP

Related Classes of org.geoforge.guillcogcecl.thread.GfrPgsThrIdsBldShtTlosGeometrySpnsAbs

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.