Exemplo n.º 1
0
 public void backgroundImpl() {
   if (backgroundAlpha) {
     // Create a small array that can be used to set the pixels several times.
     // Using a single-pixel line of length 'width' is a tradeoff between
     // speed (setting each pixel individually is too slow) and memory
     // (an array for width*height would waste lots of memory if it stayed
     // resident, and would terrify the gc if it were re-created on each trip
     // to background().
     WritableRaster raster = ((BufferedImage) image).getRaster();
     if ((clearPixels == null) || (clearPixels.length < width)) {
       clearPixels = new int[width];
     }
     java.util.Arrays.fill(clearPixels, backgroundColor);
     for (int i = 0; i < height; i++) {
       raster.setDataElements(0, i, width, 1, clearPixels);
     }
   } else {
     // new Exception().printStackTrace(System.out);
     // in case people do transformations before background(),
     // need to handle this with a push/reset/pop
     pushMatrix();
     resetMatrix();
     g2.setColor(new Color(backgroundColor)); // , backgroundAlpha));
     g2.fillRect(0, 0, width, height);
     popMatrix();
   }
 }
    @Override
    public void actionPerformed(ActionEvent e) {
      if (lastSelected == null) {
        return;
      }
      defaulted.add(lastSelected);

      BibtexEntryType type = BibtexEntryType.getStandardType(lastSelected);
      if (type != null) {
        String[] rf = type.getRequiredFieldsForCustomization(), of = type.getOptionalFields();
        List<String> req, opt1, opt2;
        if (rf != null) {
          req = java.util.Arrays.asList(rf);
        } else {
          req = new ArrayList<String>();
        }

        opt1 = new ArrayList<String>();
        opt2 = new ArrayList<String>();
        if (biblatexMode) {
          if (of != null) {
            String[] priOptArray = type.getPrimaryOptionalFields();
            String[] secOptArray = Util.getRemainder(of, priOptArray);
            if (priOptArray != null) {
              opt1 = java.util.Arrays.asList(priOptArray);
            }
            if (secOptArray != null) {
              opt2 = java.util.Arrays.asList(secOptArray);
            }
          }
        } else {
          if (of != null) {
            opt1 = java.util.Arrays.asList(of);
          }
        }

        reqComp.setFields(req);
        reqComp.setEnabled(true);
        optComp.setFields(opt1);
        if (biblatexMode) {
          optComp2.setFields(opt2);
        }
      }
    }
Exemplo n.º 3
0
 public boolean equals(Object other) {
   if (other == this) {
     return true;
   }
   if (!(other instanceof QTNode)) {
     return false;
   }
   QTNode comparee = (QTNode) other;
   return super.equals(other) && java.util.Arrays.equals(children, comparee.children);
 }
 public com.kingdee.bos.ctrl.swing.KDToolBar[] getUIMultiToolBar() {
   java.util.List list = new java.util.ArrayList();
   com.kingdee.bos.ctrl.swing.KDToolBar[] bars = super.getUIMultiToolBar();
   if (bars != null) {
     list.addAll(java.util.Arrays.asList(bars));
   }
   list.add(this.toolBar);
   return (com.kingdee.bos.ctrl.swing.KDToolBar[])
       list.toArray(new com.kingdee.bos.ctrl.swing.KDToolBar[list.size()]);
 }
Exemplo n.º 5
0
 public void actionPerformed(ActionEvent event) {
   int action = Integer.parseInt(event.getActionCommand());
   switch (action) {
     case DELETE:
       model.removeAll(java.util.Arrays.asList(list.getSelectedValues()));
       break;
     case ADD:
       if (callback != null) callback.actionPerformed(event);
       else {
         activeView.reset();
         activeView.init(activeElement);
         addElementPanel.setVisible(true);
       }
       break;
     case CANCEL:
       addElementPanel.setVisible(false);
       break;
     case ADD_ELEMENT:
       if (activeView != null) model.add(activeView.getElement());
       break;
   }
 }
public class EntryCustomizationDialog2 extends JDialog
    implements ListSelectionListener, ActionListener {

  private final JabRefFrame frame;
  protected GridBagLayout gbl = new GridBagLayout();
  protected GridBagConstraints con = new GridBagConstraints();
  private FieldSetComponent reqComp;
  private FieldSetComponent optComp;
  private FieldSetComponent optComp2;
  private EntryTypeList typeComp;
  private JButton ok;
  private JButton cancel;
  private JButton apply;
  protected JButton helpButton;
  protected JButton delete;
  protected JButton importTypes;
  protected JButton exportTypes;
  private final List<String> preset = java.util.Arrays.asList(BibtexFields.getAllFieldNames());
  private String lastSelected = null;
  private final Map<String, List<String>> reqLists = new HashMap<String, List<String>>();
  private final Map<String, List<String>> optLists = new HashMap<String, List<String>>();
  private final Map<String, List<String>> opt2Lists = new HashMap<String, List<String>>();
  private final Set<String> defaulted = new HashSet<String>();
  private final Set<String> changed = new HashSet<String>();

  private boolean biblatexMode;

  /** Creates a new instance of EntryCustomizationDialog2 */
  public EntryCustomizationDialog2(JabRefFrame frame) {
    super(frame, Globals.lang("Customize entry types"), false);

    this.frame = frame;
    initGui();
  }

  private void initGui() {
    Container pane = getContentPane();
    pane.setLayout(new BorderLayout());

    biblatexMode = Globals.prefs.getBoolean("biblatexMode");

    JPanel main = new JPanel(), buttons = new JPanel(), right = new JPanel();
    main.setLayout(new BorderLayout());
    right.setLayout(new GridLayout(biblatexMode ? 2 : 1, 2));

    java.util.List<String> entryTypes = new ArrayList<String>();
    for (String s : BibtexEntryType.ALL_TYPES.keySet()) {
      entryTypes.add(s);
    }

    typeComp = new EntryTypeList(entryTypes);
    typeComp.addListSelectionListener(this);
    typeComp.addAdditionActionListener(this);
    typeComp.addDefaultActionListener(new DefaultListener());
    typeComp.setListSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    // typeComp.setEnabled(false);
    reqComp =
        new FieldSetComponent(
            Globals.lang("Required fields"), new ArrayList<String>(), preset, true, true);
    reqComp.setEnabled(false);
    reqComp.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
    ListDataListener dataListener = new DataListener();
    reqComp.addListDataListener(dataListener);
    optComp =
        new FieldSetComponent(
            Globals.lang("Optional fields"), new ArrayList<String>(), preset, true, true);
    optComp.setEnabled(false);
    optComp.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
    optComp.addListDataListener(dataListener);
    right.add(reqComp);
    right.add(optComp);

    if (biblatexMode) {
      optComp2 =
          new FieldSetComponent(
              Globals.lang("Optional fields") + " 2", new ArrayList<String>(), preset, true, true);
      optComp2.setEnabled(false);
      optComp2.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
      optComp2.addListDataListener(dataListener);
      right.add(new JPanel());
      right.add(optComp2);
    }

    // right.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
    // Globals.lang("Fields")));
    right.setBorder(BorderFactory.createEtchedBorder());
    ok = new JButton("Ok");
    cancel = new JButton(Globals.lang("Cancel"));
    apply = new JButton(Globals.lang("Apply"));
    ok.addActionListener(this);
    apply.addActionListener(this);
    cancel.addActionListener(this);
    ButtonBarBuilder bb = new ButtonBarBuilder(buttons);
    buttons.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
    bb.addGlue();
    bb.addButton(ok);
    bb.addButton(apply);
    bb.addButton(cancel);
    bb.addGlue();

    AbstractAction closeAction =
        new AbstractAction() {

          @Override
          public void actionPerformed(ActionEvent e) {
            dispose();
          }
        };
    ActionMap am = main.getActionMap();
    InputMap im = main.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    im.put(Globals.prefs.getKey("Close dialog"), "close");
    am.put("close", closeAction);

    // con.fill = GridBagConstraints.BOTH;
    // con.weightx = 0.3;
    // con.weighty = 1;
    // gbl.setConstraints(typeComp, con);
    main.add(typeComp, BorderLayout.WEST);
    main.add(right, BorderLayout.CENTER);
    main.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    pane.add(main, BorderLayout.CENTER);
    pane.add(buttons, BorderLayout.SOUTH);
    pack();
  }

  @Override
  public void valueChanged(ListSelectionEvent e) {
    if (e.getValueIsAdjusting()) {
      return;
    }

    if (lastSelected != null) {
      // The entry type lastSelected is now unselected, so we store the current settings
      // for that type in our two maps.
      reqLists.put(lastSelected, reqComp.getFields());
      optLists.put(lastSelected, optComp.getFields());
      if (biblatexMode) {
        opt2Lists.put(lastSelected, optComp2.getFields());
      }
    }

    String s = typeComp.getFirstSelected();
    if (s == null) {
      return;
    }
    List<String> rl = reqLists.get(s);
    if (rl == null) {
      BibtexEntryType type = BibtexEntryType.getType(s);
      if (type != null) {
        String[] rf = type.getRequiredFieldsForCustomization(), of = type.getOptionalFields();
        List<String> req, opt;
        if (rf != null) {
          req = java.util.Arrays.asList(rf);
        } else {
          req = new ArrayList<String>();
        }

        if (!biblatexMode) {
          if (of != null) {
            opt = java.util.Arrays.asList(of);
          } else {
            opt = new ArrayList<String>();
          }
        } else {
          String[] priOf = type.getPrimaryOptionalFields();
          if (priOf != null) {
            opt = java.util.Arrays.asList(priOf);
          } else {
            opt = new ArrayList<String>();
          }
          List<String> opt2 = new ArrayList<String>();
          if (of != null) {
            for (String anOf : of) {
              if (!opt.contains(anOf)) {
                opt2.add(anOf);
              }
            }
          }
          optComp2.setFields(opt2);
          optComp2.setEnabled(true);
        }

        reqComp.setFields(req);
        reqComp.setEnabled(true);
        optComp.setFields(opt);
        optComp.setEnabled(true);
      } else {
        // New entry, veintle
        reqComp.setFields(new ArrayList<String>());
        reqComp.setEnabled(true);
        optComp.setFields(new ArrayList<String>());
        optComp.setEnabled(true);
        if (biblatexMode) {
          optComp2.setFields(new ArrayList<String>());
          optComp2.setEnabled(true);
        }
        new FocusRequester(reqComp);
      }
    } else {
      reqComp.setFields(rl);
      optComp.setFields(optLists.get(s));
      if (biblatexMode) {
        optComp2.setFields(opt2Lists.get(s));
      }
    }

    lastSelected = s;
    typeComp.enable(s, changed.contains(lastSelected) && !defaulted.contains(lastSelected));
  }

  private void applyChanges() {
    valueChanged(new ListSelectionEvent(new JList(), 0, 0, false));
    // Iterate over our map of required fields, and list those types if necessary:

    List<String> types = typeComp.getFields();
    for (Map.Entry<String, List<String>> stringListEntry : reqLists.entrySet()) {
      if (!types.contains(stringListEntry.getKey())) {
        continue;
      }

      List<String> reqFields = stringListEntry.getValue();
      List<String> optFields = optLists.get(stringListEntry.getKey());
      List<String> opt2Fields = opt2Lists.get(stringListEntry.getKey());
      String[] reqStr = new String[reqFields.size()];
      reqStr = reqFields.toArray(reqStr);
      String[] optStr = new String[optFields.size()];
      optStr = optFields.toArray(optStr);
      String[] opt2Str;
      if (opt2Fields != null) {
        opt2Str = opt2Fields.toArray(new String[opt2Fields.size()]);
      } else {
        opt2Str = new String[0];
      }

      // If this type is already existing, check if any changes have
      // been made
      boolean changesMade = true;

      if (defaulted.contains(stringListEntry.getKey())) {
        // This type should be reverted to its default setup.
        // System.out.println("Defaulting: "+typeName);
        String nm = StringUtil.nCase(stringListEntry.getKey());
        BibtexEntryType.removeType(nm);

        updateTypesForEntries(nm);
        continue;
      }

      BibtexEntryType oldType = BibtexEntryType.getType(stringListEntry.getKey());
      if (oldType != null) {
        String[] oldReq = oldType.getRequiredFields(), oldOpt = oldType.getOptionalFields();
        if (biblatexMode) {
          String[] priOpt = oldType.getPrimaryOptionalFields();
          String[] secOpt = Util.getRemainder(oldOpt, priOpt);
          if (equalArrays(oldReq, reqStr)
              && equalArrays(oldOpt, optStr)
              && equalArrays(secOpt, opt2Str)) {
            changesMade = false;
          }
        } else if (equalArrays(oldReq, reqStr) && equalArrays(oldOpt, optStr)) {
          changesMade = false;
        }
      }

      if (changesMade) {
        // System.out.println("Updating: "+typeName);
        CustomEntryType typ =
            biblatexMode
                ? new CustomEntryType(
                    StringUtil.nCase(stringListEntry.getKey()), reqStr, optStr, opt2Str)
                : new CustomEntryType(StringUtil.nCase(stringListEntry.getKey()), reqStr, optStr);

        BibtexEntryType.ALL_TYPES.put(stringListEntry.getKey().toLowerCase(), typ);
        updateTypesForEntries(typ.getName());
      }
    }

    Set<Object> toRemove = new HashSet<Object>();
    for (String o : BibtexEntryType.ALL_TYPES.keySet()) {
      if (!types.contains(o)) {
        toRemove.add(o);
      }
    }

    // Remove those that should be removed:
    if (!toRemove.isEmpty()) {
      for (Object aToRemove : toRemove) {
        typeDeletion((String) aToRemove);
      }
    }

    updateTables();
  }

  private void typeDeletion(String name) {
    BibtexEntryType type = BibtexEntryType.getType(name);

    if (type instanceof CustomEntryType) {
      if (BibtexEntryType.getStandardType(name) == null) {
        int reply =
            JOptionPane.showConfirmDialog(
                frame,
                Globals.lang(
                    "All entries of this " + "type will be declared " + "typeless. Continue?"),
                Globals.lang("Delete custom format") + " '" + StringUtil.nCase(name) + '\'',
                JOptionPane.YES_NO_OPTION,
                JOptionPane.WARNING_MESSAGE);
        if (reply != JOptionPane.YES_OPTION) {
          return;
        }
      }
      BibtexEntryType.removeType(name);
      updateTypesForEntries(StringUtil.nCase(name));
      changed.remove(name);
      reqLists.remove(name);
      optLists.remove(name);
      if (biblatexMode) {
        opt2Lists.remove(name);
      }
    }
    // messageLabel.setText("'"+type.getName()+"' "+
    //        Globals.lang("is a standard type."));

  }

  private boolean equalArrays(String[] one, String[] two) {
    if ((one == null) && (two == null)) {
      return true; // Both null.
    }
    if ((one == null) || (two == null)) {
      return false; // One of them null, the other not.
    }
    if (one.length != two.length) {
      return false; // Different length.
    }
    // If we get here, we know that both are non-null, and that they have the same length.
    for (int i = 0; i < one.length; i++) {
      if (!one[i].equals(two[i])) {
        return false;
      }
    }
    // If we get here, all entries have matched.
    return true;
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == ok) {
      applyChanges();
      dispose();
    } else if (e.getSource() == cancel) {
      dispose();
    } else if (e.getSource() == apply) {
      applyChanges();
    } else if (e.getSource() == typeComp) {
      // System.out.println("add: "+e.getActionCommand());
      typeComp.selectField(e.getActionCommand());
    }
  }

  /**
   * Cycle through all databases, and make sure everything is updated with the new type
   * customization. This includes making sure all entries have a valid type, that no obsolete entry
   * editors are around, and that the right-click menus' change type menu is up-to-date.
   */
  private void updateTypesForEntries(String typeName) {
    if (frame.getTabbedPane().getTabCount() == 0) {
      return;
    }
    for (int i = 0; i < frame.getTabbedPane().getTabCount(); i++) {
      BasePanel bp = (BasePanel) frame.getTabbedPane().getComponentAt(i);

      // Invalidate associated cached entry editor
      bp.entryEditors.remove(typeName);

      for (BibtexEntry entry : bp.database().getEntries()) {
        entry.updateType();
      }
    }
  }

  private void updateTables() {
    if (frame.getTabbedPane().getTabCount() == 0) {
      return;
    }
    for (int i = 0; i < frame.getTabbedPane().getTabCount(); i++) {
      frame.getTabbedPane().getComponentAt(i);
    }
  }

  // DEFAULT button pressed. Remember that this entry should be reset to default,
  // unless changes are made later.
  private class DefaultListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
      if (lastSelected == null) {
        return;
      }
      defaulted.add(lastSelected);

      BibtexEntryType type = BibtexEntryType.getStandardType(lastSelected);
      if (type != null) {
        String[] rf = type.getRequiredFieldsForCustomization(), of = type.getOptionalFields();
        List<String> req, opt1, opt2;
        if (rf != null) {
          req = java.util.Arrays.asList(rf);
        } else {
          req = new ArrayList<String>();
        }

        opt1 = new ArrayList<String>();
        opt2 = new ArrayList<String>();
        if (biblatexMode) {
          if (of != null) {
            String[] priOptArray = type.getPrimaryOptionalFields();
            String[] secOptArray = Util.getRemainder(of, priOptArray);
            if (priOptArray != null) {
              opt1 = java.util.Arrays.asList(priOptArray);
            }
            if (secOptArray != null) {
              opt2 = java.util.Arrays.asList(secOptArray);
            }
          }
        } else {
          if (of != null) {
            opt1 = java.util.Arrays.asList(of);
          }
        }

        reqComp.setFields(req);
        reqComp.setEnabled(true);
        optComp.setFields(opt1);
        if (biblatexMode) {
          optComp2.setFields(opt2);
        }
      }
    }
  }

  class DataListener implements ListDataListener {

    @Override
    public void intervalAdded(javax.swing.event.ListDataEvent e) {
      record();
    }

    @Override
    public void intervalRemoved(javax.swing.event.ListDataEvent e) {
      record();
    }

    @Override
    public void contentsChanged(javax.swing.event.ListDataEvent e) {
      record();
    }

    private void record() {
      if (lastSelected == null) {
        return;
      }
      defaulted.remove(lastSelected);
      changed.add(lastSelected);
      typeComp.enable(lastSelected, true);
    }
  }
}
  @Override
  public void valueChanged(ListSelectionEvent e) {
    if (e.getValueIsAdjusting()) {
      return;
    }

    if (lastSelected != null) {
      // The entry type lastSelected is now unselected, so we store the current settings
      // for that type in our two maps.
      reqLists.put(lastSelected, reqComp.getFields());
      optLists.put(lastSelected, optComp.getFields());
      if (biblatexMode) {
        opt2Lists.put(lastSelected, optComp2.getFields());
      }
    }

    String s = typeComp.getFirstSelected();
    if (s == null) {
      return;
    }
    List<String> rl = reqLists.get(s);
    if (rl == null) {
      BibtexEntryType type = BibtexEntryType.getType(s);
      if (type != null) {
        String[] rf = type.getRequiredFieldsForCustomization(), of = type.getOptionalFields();
        List<String> req, opt;
        if (rf != null) {
          req = java.util.Arrays.asList(rf);
        } else {
          req = new ArrayList<String>();
        }

        if (!biblatexMode) {
          if (of != null) {
            opt = java.util.Arrays.asList(of);
          } else {
            opt = new ArrayList<String>();
          }
        } else {
          String[] priOf = type.getPrimaryOptionalFields();
          if (priOf != null) {
            opt = java.util.Arrays.asList(priOf);
          } else {
            opt = new ArrayList<String>();
          }
          List<String> opt2 = new ArrayList<String>();
          if (of != null) {
            for (String anOf : of) {
              if (!opt.contains(anOf)) {
                opt2.add(anOf);
              }
            }
          }
          optComp2.setFields(opt2);
          optComp2.setEnabled(true);
        }

        reqComp.setFields(req);
        reqComp.setEnabled(true);
        optComp.setFields(opt);
        optComp.setEnabled(true);
      } else {
        // New entry, veintle
        reqComp.setFields(new ArrayList<String>());
        reqComp.setEnabled(true);
        optComp.setFields(new ArrayList<String>());
        optComp.setEnabled(true);
        if (biblatexMode) {
          optComp2.setFields(new ArrayList<String>());
          optComp2.setEnabled(true);
        }
        new FocusRequester(reqComp);
      }
    } else {
      reqComp.setFields(rl);
      optComp.setFields(optLists.get(s));
      if (biblatexMode) {
        optComp2.setFields(opt2Lists.get(s));
      }
    }

    lastSelected = s;
    typeComp.enable(s, changed.contains(lastSelected) && !defaulted.contains(lastSelected));
  }
 public List<String> getFields() {
   return java.util.Arrays.asList(fields);
 }
Exemplo n.º 9
0
  /**
   * Compute final cluster centers for all clusters using K-mean clustering method
   *
   * @param srcBandList the input bands
   * @param tileRectangles Array of rectangles for all source tiles of the image
   * @param op the operator
   */
  private void computeFinalTerrainClusterCenters(
      final double[][] fdd,
      final java.util.List<ClusterInfo> pvCenterList,
      final java.util.List<ClusterInfo> pdCenterList,
      final java.util.List<ClusterInfo> psCenterList,
      final PolBandUtils.PolSourceBand srcBandList,
      final Rectangle[] tileRectangles,
      final PolarimetricClassificationOp op) {

    boolean endIteration = false;

    final StatusProgressMonitor status =
        new StatusProgressMonitor(StatusProgressMonitor.TYPE.SUBTASK);
    status.beginTask("Computing Final Cluster Centres... ", tileRectangles.length * maxIterations);

    final int pvNumClusters = pvCenterList.size();
    final int pdNumClusters = pdCenterList.size();
    final int psNumClusters = psCenterList.size();

    final int maxNumClusters = Math.max(pvNumClusters, Math.max(pdNumClusters, psNumClusters));
    final int[][] clusterCounter = new int[3][maxNumClusters];

    final ThreadManager threadManager = new ThreadManager();

    try {
      for (int it = 0; (it < maxIterations && !endIteration); ++it) {
        // System.out.println("Iteration: " + it);

        //                final long startTime = System.nanoTime();
        //                final long endTime;
        final double[][][] pvSumRe = new double[pvNumClusters][3][3];
        final double[][][] pvSumIm = new double[pvNumClusters][3][3];
        final double[][][] pdSumRe = new double[pdNumClusters][3][3];
        final double[][][] pdSumIm = new double[pdNumClusters][3][3];
        final double[][][] psSumRe = new double[psNumClusters][3][3];
        final double[][][] psSumIm = new double[psNumClusters][3][3];

        java.util.Arrays.fill(clusterCounter[0], 0);
        java.util.Arrays.fill(clusterCounter[1], 0);
        java.util.Arrays.fill(clusterCounter[2], 0);

        for (final Rectangle rectangle : tileRectangles) {

          final Thread worker =
              new Thread() {

                final Tile[] sourceTiles = new Tile[srcBandList.srcBands.length];
                final ProductData[] dataBuffers = new ProductData[srcBandList.srcBands.length];

                final double[][] Tr = new double[3][3];
                final double[][] Ti = new double[3][3];

                @Override
                public void run() {
                  op.checkIfCancelled();

                  final int x0 = rectangle.x;
                  final int y0 = rectangle.y;
                  final int w = rectangle.width;
                  final int h = rectangle.height;
                  final int xMax = x0 + w;
                  final int yMax = y0 + h;

                  final Rectangle sourceRectangle = getSourceRectangle(x0, y0, w, h);
                  for (int i = 0; i < sourceTiles.length; ++i) {
                    sourceTiles[i] = op.getSourceTile(srcBandList.srcBands[i], sourceRectangle);
                    dataBuffers[i] = sourceTiles[i].getDataBuffer();
                  }
                  final TileIndex srcIndex = new TileIndex(sourceTiles[0]);

                  for (int y = y0; y < yMax; ++y) {
                    for (int x = x0; x < xMax; ++x) {

                      PolOpUtils.getMeanCoherencyMatrix(
                          x,
                          y,
                          halfWindowSizeX,
                          halfWindowSizeY,
                          srcWidth,
                          srcHeight,
                          sourceProductType,
                          srcIndex,
                          dataBuffers,
                          Tr,
                          Ti);

                      int clusterIdx;
                      synchronized (clusterCounter) {
                        if (mask[y][x] < -64) { // pv
                          clusterIdx = findClosestCluster(Tr, Ti, pvCenterList);
                          computeSummationOfT3(clusterIdx + 1, Tr, Ti, pvSumRe, pvSumIm);
                          clusterCounter[0][clusterIdx] += 1;
                          mask[y][x] = (byte) (-128 + clusterIdx);

                        } else if (mask[y][x] < 0) { // pd
                          clusterIdx = findClosestCluster(Tr, Ti, pdCenterList);
                          computeSummationOfT3(clusterIdx + 1, Tr, Ti, pdSumRe, pdSumIm);
                          clusterCounter[1][clusterIdx] += 1;
                          mask[y][x] = (byte) (-64 + clusterIdx);

                        } else if (mask[y][x] < 64) { // ps
                          clusterIdx = findClosestCluster(Tr, Ti, psCenterList);
                          computeSummationOfT3(clusterIdx + 1, Tr, Ti, psSumRe, psSumIm);
                          clusterCounter[2][clusterIdx] += 1;
                          mask[y][x] = (byte) clusterIdx;

                        } else { // mixed

                          java.util.List<ClusterInfo> allCenterList = new ArrayList<>();
                          allCenterList.addAll(pvCenterList);
                          allCenterList.addAll(pdCenterList);
                          allCenterList.addAll(psCenterList);
                          clusterIdx = findClosestCluster(Tr, Ti, allCenterList);

                          if (clusterIdx >= pvNumClusters + pdNumClusters) { // ps
                            clusterIdx -= pvNumClusters + pdNumClusters;
                            computeSummationOfT3(clusterIdx + 1, Tr, Ti, psSumRe, psSumIm);
                            clusterCounter[2][clusterIdx] += 1;
                            mask[y][x] = (byte) clusterIdx;

                          } else if (clusterIdx >= pvNumClusters) { // pd
                            clusterIdx -= pvNumClusters;
                            computeSummationOfT3(clusterIdx + 1, Tr, Ti, pdSumRe, pdSumIm);
                            clusterCounter[1][clusterIdx] += 1;
                            mask[y][x] = (byte) (-64 + clusterIdx);

                          } else { // pv
                            computeSummationOfT3(clusterIdx + 1, Tr, Ti, pvSumRe, pvSumIm);
                            clusterCounter[0][clusterIdx] += 1;
                            mask[y][x] = (byte) (-128 + clusterIdx);
                          }
                        }
                      }
                    }
                  }
                }
              };
          threadManager.add(worker);

          status.worked(1);
        }
        threadManager.finish();

        /*
        endTime = System.nanoTime();
        final long duration = endTime - startTime;
        System.out.println("duration = " + duration);
        */
        updateClusterCenter(pvCenterList, clusterCounter[0], pvSumRe, pvSumIm);
        updateClusterCenter(pdCenterList, clusterCounter[1], pdSumRe, pdSumIm);
        updateClusterCenter(psCenterList, clusterCounter[2], psSumRe, psSumIm);
      }
      /*
      System.out.println("# of clusters in Pv: " + pvNumClusters);
      System.out.print("Pixels in each Pv cluster: ");
      for (int i = 0; i < pvNumClusters; i++) {
          System.out.print(clusterCounter[0][i] + ", ");
      }
      System.out.println();
      System.out.println("# of clusters in Pd: " + pdNumClusters);
      System.out.print("Pixels in each Pd cluster: ");
      for (int i = 0; i < pdNumClusters; i++) {
          System.out.print(clusterCounter[1][i] + ", ");
      }
      System.out.println();
      System.out.println("# of clusters in Ps: " + psNumClusters);
      System.out.print("Pixels in each Ps cluster: ");
      for (int i = 0; i < psNumClusters; i++) {
          System.out.print(clusterCounter[2][i] + ", ");
      }
      System.out.println();
      */

      // todo the average cluster power should be used in colour selection for each cluster, not
      // used now
      // compute average power for each cluster
      final double[] pvAvgClusterPower = new double[pvNumClusters];
      final double[] pdAvgClusterPower = new double[pdNumClusters];
      final double[] psAvgClusterPower = new double[psNumClusters];

      int clusterIdx = -1;
      for (int y = 0; y < srcHeight; y++) {
        for (int x = 0; x < srcWidth; x++) {
          if (mask[y][x] < -64) { // pv
            clusterIdx = mask[y][x] + 128;
            pvAvgClusterPower[clusterIdx] += fdd[y][x];
          } else if (mask[y][x] < 0) { // pd
            clusterIdx = mask[y][x] + 64;
            pdAvgClusterPower[clusterIdx] += fdd[y][x];
          } else { // ps
            clusterIdx = mask[y][x];
            psAvgClusterPower[clusterIdx] += fdd[y][x];
          }
        }
      }

      for (int c = 0; c < pvNumClusters; c++) {
        pvAvgClusterPower[c] /= clusterCounter[0][c];
      }

      for (int c = 0; c < pdNumClusters; c++) {
        pdAvgClusterPower[c] /= clusterCounter[1][c];
      }

      for (int c = 0; c < psNumClusters; c++) {
        psAvgClusterPower[c] /= clusterCounter[2][c];
      }

      // map cluster index to colour index, colour index ranges for the 3 categories are given by
      // surf: [0,29], vol: [30, 59] and dbl: [60,89], currently the clusters are evenly distributed
      // in the
      // colour range
      pvColourIndexMap = new int[pvNumClusters];
      pdColourIndexMap = new int[pdNumClusters];
      psColourIndexMap = new int[psNumClusters];
      for (int c = 0; c < pvNumClusters; c++) {
        pvColourIndexMap[c] =
            numInitialClusters + getColourIndex(c, pvAvgClusterPower, numInitialClusters) + 1;
      }
      for (int c = 0; c < pdNumClusters; c++) {
        pdColourIndexMap[c] =
            2 * numInitialClusters + getColourIndex(c, pdAvgClusterPower, numInitialClusters) + 1;
      }
      for (int c = 0; c < psNumClusters; c++) {
        psColourIndexMap[c] = getColourIndex(c, psAvgClusterPower, numInitialClusters) + 1;
      }

    } catch (Throwable e) {
      OperatorUtils.catchOperatorException(op.getId() + " computeInitialClusterCenters ", e);
    } finally {
      status.done();
    }
  }
Exemplo n.º 10
0
  // Is used if one Territory attacks another returns true if ownedby player has won
  public boolean Attack(Territory defterritory) {

    // Used Variables get initialized
    int attackarmys;
    int defarmys;

    // Get positioned armys on this territory which is attacking and cap them on 3
    attackarmys = (this.CheckArmyCount() - 1);
    if (attackarmys >= 3) {
      attackarmys = 3;
    }
    // Get positioned armys on the other territory which is defending and cap them on 2
    defarmys = (defterritory.CheckArmyCount());
    if (defarmys >= 2) {
      defarmys = 2;
    }
    // If 1 or more army are attacking
    if (attackarmys >= 1) {

      // Create Random Generator
      Random randomGenerator = new Random();

      // Create Arrays for representing the dices
      int[] attackeyes = new int[attackarmys];
      int[] defeyes = new int[defarmys];

      // Get random numbers for the attackdices
      for (int i = 0; i < attackarmys; i++) {
        attackeyes[i] = randomGenerator.nextInt(6) + 1;
      }
      // Get random numbers for the defensedices
      for (int i = 0; i < defarmys; i++) {
        defeyes[i] = randomGenerator.nextInt(6) + 1;
      }
      // Sort the Arrays by number
      java.util.Arrays.sort(attackeyes);
      java.util.Arrays.sort(defeyes);

      // Go through the arrays starting with the highest element
      for (int i = 1; i < (defarmys + 1) && i < (attackarmys + 1); i++) {
        // Check if the attackdice is greater than the defensedice
        if (attackeyes[attackarmys - i] > defeyes[defarmys - i]) {
          System.out.println(
              "Rollwin:"
                  + i
                  + " - Eyes:"
                  + attackeyes[attackarmys - i]
                  + "/"
                  + defeyes[defarmys - i]);
          // If 1 army of the defending territory was removed and the corresponding army was beaten
          // (completely)
          if (defterritory.removeArmies(1, this.ownedby)) {
            // Take over the territory with the attacking armys
            defterritory.enforceArmies(attackarmys - i, this.ownedby);
            // And remove them by their former territory
            this.removeArmies(attackarmys - i, this.ownedby);
            // Return true for a successful attack
            return true;
          }

        } else {
          System.out.println(
              "Rolllose:"
                  + i
                  + " - Eyes:"
                  + attackeyes[attackarmys - i]
                  + "/"
                  + defeyes[defarmys - i]);
          // Else remove one army of the attackers
          this.removeArmies(1, this.ownedby);
        }
      }
    }
    return false;
  }