OwlClassSelectorApplet.java
Go to the documentation of this file.
00001 package edu.tum.cs.ias.knowrob.vis.applets;
00002 
00003 import java.awt.Color;
00004 import java.awt.Cursor;
00005 import java.awt.event.KeyEvent;
00006 import java.awt.event.MouseEvent;
00007 import java.awt.event.MouseListener;
00008 import java.awt.event.MouseMotionListener;
00009 import java.util.ArrayList;
00010 import java.util.Collections;
00011 import java.util.LinkedHashMap;
00012 import java.util.List;
00013 import java.util.Map;
00014 
00015 import javax.vecmath.Vector2f;
00016 
00017 import controlP5.ControlEvent;
00018 import controlP5.ControlP5;
00019 import controlP5.Controller;
00020 import controlP5.MultiList;
00021 import controlP5.MultiListButton;
00022 import controlP5.Textfield;
00023 import edu.tum.cs.ias.knowrob.owl.OWLClass;
00024 import edu.tum.cs.ias.knowrob.vis.themes.GreyTheme;
00025 
00026 import processing.core.PApplet;
00027 
00028 
00035 public class OwlClassSelectorApplet  extends PApplet implements MouseListener, MouseMotionListener {
00036 
00037         private static final long serialVersionUID = 7695328948788620463L;
00038         
00039 
00043         public ControlP5 controlP5;
00044 
00048         MultiList class_listbox ;
00049         
00053         private Vector2f draggingStart;
00054 
00058         private static final Cursor normalCursor = new Cursor(Cursor.DEFAULT_CURSOR);
00059         
00063         private static final Cursor moveCursor = new Cursor(Cursor.MOVE_CURSOR);
00064 
00065         
00066         
00070         private List<OWLClass> owl_classes;
00071 
00075         private Map<Float, OWLClass> id2class;
00076         
00080         private Map<String, MultiListButton> act2button;
00081         
00085         private OWLClass searchResult = null;
00086 
00090         private IClassSelectionCallback cb;
00091 
00092 
00093         private Textfield search;
00094 
00095         
00099         @Override
00100         public void setup()
00101         {
00102                 size(800, 600);
00103 
00104                 if (this.frame != null)
00105                 {
00106                         this.frame.setTitle("OWL class selector");
00107                         this.frame.setBackground(new Color(10, 10, 10));
00108                 }
00109 
00110                 frameRate(10);
00111                 
00112                 id2class = new LinkedHashMap<Float, OWLClass>();
00113                 act2button = new LinkedHashMap<String, MultiListButton>();
00114                 
00115                 
00116         }
00117 
00118 
00122         @Override
00123         public void draw() {
00124 
00125                 background(50);
00126 
00127                 if(controlP5!=null) {
00128                         synchronized(controlP5) {
00129                                 controlP5.draw();
00130                         }
00131                 }
00132         }
00133 
00134 
00135 
00136         public void setBaseClass(String base) {
00137                 
00138                 owl_classes = Collections.synchronizedList(new ArrayList<OWLClass>());
00139 
00140                 OWLClass basecl = OWLClass.getOWLClass(base);
00141                 basecl.readFromProlog();
00142                 owl_classes.addAll(basecl.getSubclasses());
00143 
00144                 initControlP5();
00145                 
00146         }
00147 
00148         
00149         // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // 
00150         // 
00151         //  Searching for OWL classes
00152         // 
00153         
00154         
00163         private OWLClass searchOWLClass(String stringValue, List<OWLClass> owl_classes) {
00164                 
00165                 synchronized (owl_classes) {
00166                         for(OWLClass a : owl_classes) {
00167                                 
00168                                 if(a.getIRI().toLowerCase().contains(stringValue.toLowerCase())) {
00169                                         
00170                                         showSearchResult(a);
00171                                         return a;
00172                                         
00173                                 } else if(a.getLabel().toLowerCase().contains(stringValue.toLowerCase())) {
00174 
00175                                         showSearchResult(a);
00176                                         class_listbox.update();
00177                                         
00178                                         return a;
00179                                         
00180                                 } else if(a.getSubclasses().size()>0){
00181                                         
00182                                         OWLClass found = searchOWLClass(stringValue, a.getSubclasses());
00183                                         
00184                                         if(found!=null) {
00185                                                 return found;
00186                                         }
00187                                 }
00188                         }
00189                 }
00190 
00191                 return null;
00192         }
00193 
00194 
00195 
00196         private void showSearchResult(OWLClass a) {
00197                 
00198                 // reset highlight
00199                 if(searchResult!=null && act2button.get(searchResult.getIRI())!=null)
00200                         act2button.get(searchResult.getIRI()).setColorBackground(80);
00201                 
00202                 // try to open the right tab
00203                 OWLClass tmp = a;
00204                 while((tmp.getSuperClasses() != null) && 
00205                          (!tmp.getSuperClasses().isEmpty()) &&
00206                          (act2button.get(tmp.getSuperClasses().firstElement().getIRI()) !=null)) {
00207                         
00208                         act2button.get(tmp.getSuperClasses().firstElement().getIRI()).open();
00209                         tmp = tmp.getSuperClasses().firstElement();
00210                 }
00211                 
00212                 
00213                 // highlight the result
00214                 if(act2button!=null && act2button.get(a.getIRI())!=null)
00215                         act2button.get(a.getIRI()).setColorBackground(color(180));
00216                 searchResult = a;
00217                 class_listbox.update();
00218         }
00219 
00220         
00221 
00222         
00223         // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // 
00224         // 
00225         //  Event handling
00226         // 
00227         
00228 
00229         public void controlEvent(ControlEvent ev) {
00230                 
00231                 // submit selection with ENTER if selection has been made, search otherwise
00232                 if(ev.getController().getName().equals("search")) {
00233 
00234                         if(searchResult!=null) {
00235                                 cb.owlClassSelected(searchResult.getIRI());
00236                                 this.frame.setVisible(false);
00237                         } else {
00238                                 searchOWLClass(ev.getStringValue(), owl_classes);
00239                         }
00240                         return;
00241                 }
00242                 
00243                 if(ev.getController().getName().equals("class list")) {
00244                         
00245                         if(this.cb!=null && id2class!=null) {
00246                                 cb.owlClassSelected(id2class.get(Float.valueOf(ev.getValue())).getIRI());
00247                                 this.frame.setVisible(false);
00248                                 
00249                         } else {
00250                                 System.out.println("Selected class " + id2class.get(Float.valueOf(ev.getValue())).getIRI());
00251                         }
00252                 }
00253         }
00254         
00255         public void keyPressed(KeyEvent e) {
00256                 
00257                 // search in class structure with TAB
00258                 if(e.getKeyCode() == KeyEvent.VK_TAB) {
00259                         searchOWLClass(((Textfield) search).getText(), owl_classes);
00260                         return;
00261                 } else {
00262                         controlP5.keyHandler.keyEvent(e, controlP5.controlWindow, true);
00263                 }
00264         }
00265         
00266         @Override
00267         public void mouseDragged(MouseEvent e) {
00268 
00269                 if (draggingStart != null) {
00270 
00271                         this.class_listbox.updateLocation(e.getX() - draggingStart.x,
00272                                                                                           e.getY() - draggingStart.y);
00273 
00274                         draggingStart.x = e.getX();
00275                         draggingStart.y = e.getY();
00276                 }
00277                 
00278                 // only forward left-clicks to ControlP5 
00279                 // (no class selection by right click/dragging)
00280                 if (e.getButton() == MouseEvent.BUTTON1) {
00281                         controlP5.controlWindow.mouseEvent(e);
00282                 }
00283         }
00284 
00285         @Override
00286         public void mousePressed(MouseEvent e) {
00287 
00288                 if (e.getButton() == MouseEvent.BUTTON3) {
00289 
00290                         draggingStart = new Vector2f(e.getX(),e.getY());
00291                         setCursor(moveCursor);
00292 
00293                 } else {
00294                         setCursor(normalCursor);
00295                 }
00296                 
00297                 // only forward left-clicks to ControlP5 
00298                 // (no class selection by right click/dragging)
00299                 if (e.getButton() == MouseEvent.BUTTON1) {
00300                         controlP5.controlWindow.mouseEvent(e);
00301                 }
00302         }
00303 
00304         @Override
00305         public void mouseReleased(MouseEvent e) {
00306                 setCursor(normalCursor);
00307                 draggingStart = null;
00308                 
00309                 // only forward left-clicks to ControlP5 
00310                 // (no class selection by right click/dragging)
00311                 if (e.getButton() == MouseEvent.BUTTON1) {
00312                         controlP5.controlWindow.mouseEvent(e);
00313                 }
00314         }
00315 
00316         public void mouseClicked(MouseEvent e) {
00317                 
00318                 // only forward left-clicks to ControlP5 
00319                 // (no class selection by right click/dragging)
00320                 if (e.getButton() == MouseEvent.BUTTON1) {
00321                         controlP5.controlWindow.mouseEvent(e);
00322                 }
00323         }
00324 
00325         public void setClassSelectedCallback(IClassSelectionCallback cb) {
00326                 this.cb = cb;           
00327         }
00328 
00329 
00330         
00331         
00332         // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // 
00333         // 
00334         //  Initialize GUI elements
00335         // 
00336 
00340         private void initControlP5() {
00341 
00342                 controlP5 = new ControlP5(this);
00343                 GreyTheme.applyStyle(controlP5);
00344                 
00345 
00346                 synchronized(controlP5) {
00347                         
00348                         search = controlP5.addTextfield("search", 20, 20, 200, 20).setAutoClear(false).setFocus(true);
00349                         class_listbox = GreyTheme.applyStyle(controlP5.addMultiList("class list", 20, 60, 80, 17));
00350         
00351                         float textwidth = textWidth("CheckingWhetherConditionObtains");
00352                         class_listbox.setWidth((int)textwidth + 10);
00353                         
00354                         createListButtons(owl_classes, class_listbox, 0, 0);
00355                 }
00356         }
00357         
00358         
00367         private int createListButtons(List<OWLClass> owl_classes, Controller<?> parent, int start_idx, int level) {
00368 
00369                 int idx = start_idx;
00370                 synchronized(owl_classes) {
00371                         for(OWLClass act : owl_classes) {
00372 
00373                                 MultiListButton b = null;
00374                                 if (parent instanceof MultiList) {
00375 
00376                                         b = ((MultiList)parent).add(idx + "_" + act.getIRI(), idx++);
00377                                         configureButton(b, level, act);
00378 
00379                                 } else if (parent instanceof MultiListButton) {
00380 
00381                                         b = ((MultiListButton)parent).add(idx + "_" + act.getIRI(), idx++);
00382                                         configureButton(b, level, act);
00383                                 } 
00384 
00385                                 if(act.getSubclasses().size()>0)
00386                                         idx = createListButtons(act.getSubclasses(), b, idx, level+1);
00387                         }
00388                 }
00389                 return idx;
00390         }
00391 
00392 
00393         
00401         private void configureButton(MultiListButton b, int level, OWLClass act) {
00402                 
00403                 GreyTheme.applyStyle(b);
00404                 b.setCaptionLabel(act.getLabel());
00405                 
00406                 if(level>=3)
00407                         b.getCaptionLabel().setColor(color(10));
00408                 
00409                 if(act2button==null)
00410                         act2button = new LinkedHashMap<String, MultiListButton>();
00411                 
00412                 act2button.put(act.getIRI(), b);
00413                 
00414                 if(id2class==null)
00415                         id2class = new LinkedHashMap<Float, OWLClass>();
00416                 
00417                 id2class.put(b.getValue(), act);
00418         }
00419 
00420         
00421         
00422         
00423         // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // 
00424         // 
00425         //  OWL class datastructure
00426         // 
00427 
00428 
00429 
00430         
00431 
00432         public static void main(String args[]) {
00433                 PApplet.main(new String[] { "edu.tum.cs.ias.knowrob.vis.applets.OwlClassSelectorApplet" });
00434         }
00435 
00436 
00437 
00438 
00439 }
00440 


mod_vis
Author(s): Moritz Tenorth, Jakob Engel
autogenerated on Mon Oct 6 2014 01:30:00