View Javadoc
1   // ******************************************************************************
2   //
3   // Title:       Force Field X.
4   // Description: Force Field X - Software for Molecular Biophysics.
5   // Copyright:   Copyright (c) Michael J. Schnieders 2001-2024.
6   //
7   // This file is part of Force Field X.
8   //
9   // Force Field X is free software; you can redistribute it and/or modify it
10  // under the terms of the GNU General Public License version 3 as published by
11  // the Free Software Foundation.
12  //
13  // Force Field X is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  // details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // Force Field X; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA 02111-1307 USA
21  //
22  // Linking this library statically or dynamically with other modules is making a
23  // combined work based on this library. Thus, the terms and conditions of the
24  // GNU General Public License cover the whole combination.
25  //
26  // As a special exception, the copyright holders of this library give you
27  // permission to link this library with independent modules to produce an
28  // executable, regardless of the license terms of these independent modules, and
29  // to copy and distribute the resulting executable under terms of your choice,
30  // provided that you also meet, for each linked independent module, the terms
31  // and conditions of the license of that module. An independent module is a
32  // module which is not derived from or based on this library. If you modify this
33  // library, you may extend this exception to your version of the library, but
34  // you are not obligated to do so. If you do not wish to do so, delete this
35  // exception statement from your version.
36  //
37  // ******************************************************************************
38  package ffx.ui;
39  
40  import ffx.potential.bonded.MSNode;
41  
42  import javax.swing.tree.TreeNode;
43  import java.util.ArrayList;
44  import java.util.Enumeration;
45  
46  /**
47   * The Selection class will be used to make recursive multiscale selections, however its
48   * implementation is not yet complete.
49   *
50   * @author Michael J. Schnieders
51   */
52  public class Selection {
53  
54    MSNode m;
55    private Class<? extends Object> scale;
56    private String criteria;
57    private ArrayList<MSNode> selected;
58  
59    private Selection(MSNode m, Class<? extends Object> scale, String criteria) {
60      this.scale = scale;
61      this.criteria = criteria;
62      this.m = m;
63      selected = new ArrayList<>();
64    }
65  
66    private Selection(MSNode m, String scale, String criteria) {
67      try {
68        this.scale = Class.forName(scale);
69      } catch (Exception e) {
70        this.scale = null;
71      }
72      this.criteria = criteria;
73      this.m = m;
74      selected = new ArrayList<>();
75    }
76  
77    /**
78     * select
79     *
80     * @param m        a {@link ffx.potential.bonded.MSNode} object.
81     * @param scale    a {@link java.lang.Class} object.
82     * @param criteria a {@link java.lang.String} object.
83     * @return a {@link ffx.ui.Selection} object.
84     */
85    public static Selection select(MSNode m, Class<? extends Object> scale, String criteria) {
86      Selection s = new Selection(m, scale, criteria);
87      s.evaluate();
88      return s;
89    }
90  
91    /**
92     * select
93     *
94     * @param m        a {@link ffx.potential.bonded.MSNode} object.
95     * @param scale    a {@link java.lang.String} object.
96     * @param criteria a {@link java.lang.String} object.
97     * @return a {@link ffx.ui.Selection} object.
98     */
99    public static Selection select(MSNode m, String scale, String criteria) {
100     Selection s = new Selection(m, scale, criteria);
101     s.evaluate();
102     return s;
103   }
104 
105   /**
106    * and
107    *
108    * @param scale    a {@link java.lang.Class} object.
109    * @param criteria a {@link java.lang.String} object.
110    * @return a {@link ffx.ui.Selection} object.
111    */
112   public Selection and(Class<? extends Object> scale, String criteria) {
113     this.scale = scale;
114     this.criteria = criteria;
115     evaluate();
116     return this;
117   }
118 
119   /**
120    * or
121    *
122    * @param scale    a {@link java.lang.Class} object.
123    * @param criteria a {@link java.lang.String} object.
124    * @return a {@link ffx.ui.Selection} object.
125    */
126   public Selection or(Class<? extends Object> scale, String criteria) {
127     this.scale = scale;
128     this.criteria = criteria;
129     evaluate();
130     return this;
131   }
132 
133   private void evaluate() {
134     Enumeration<TreeNode> e = m.depthFirstEnumeration();
135     while (e.hasMoreElements()) {
136       MSNode n = (MSNode) e.nextElement();
137       if (scale.isInstance(n)) {
138         selected.add(n);
139       }
140     }
141   }
142 }