Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Java: GUI и Java FX приложения > Добавить узел в пустое дерево


Автор: kaa 21.4.2008, 21:47
Есть расширенный класс ноды:

Код

class TestObject extends DefaultMutableTreeNode implements Comparable<TestObject>{
    
    private SystemTree tree;
    private int id;
    private boolean root; //Свойство показывает, что элемент не обладает никаким типом и 
                          //является всего-лиь корнем дерева.
        /*
        *int objectType:
        *0 - Theme
        *1 - Series
        *2 - Question
        *3 - Answer
        *4 - root
        */
    private int objectType;
    public static int THEME = 0;
    public static int SERIES = 1;
    public static int QUESTION = 2;
    public static int ANSWER = 3;
    public static int ROOT = 4;
        /*
        *int type(applying for question and answer only)
        *  for question
        *      0 - one-from-lot
        *      1 - some-from-lot
        *      2 - user-variant
        *  for answer
        *      0 - false
        *      1 - true
        *
        *  other -4 (THEME and SERIES only)
        */
    private int type;
    private boolean publish;
    private String name, description;
    
    /*
     *@param SystemTree tree.
     *@param int objectType.
     *@param String name.
     *@param String Description.
     *@param int type.
     *@param boolean publish.
     */
    public TestObject(SystemTree tree, int objectType, String name, String description, int type, boolean publish){
        super();
        if(objectType!=4){
            this.tree = tree;
            this.objectType = objectType;
            this.name = name;
            this.type = type;
            this.publish = publish;
            if(objectType==0){
                this.id = tree.getThemeId();
                this.description = description;
            }
            else if(objectType==1){
                this.id=tree.getSeriesId();
                this.description = description;
            }
            else if(objectType==2)
                this.id = tree.getQuestionId();
            else if(objectType==3)
                this.id = tree.getAnswerId();
        }
        else{
           super.setUserObject(name);
           this.objectType = objectType;
           this.type = 4;
        }
    }
    /*
     *@param SystemTree tree.
     *@param int objectType.
     *@param String name.
     *@param int type.
     *@param boolean publish.
     */
    public TestObject(SystemTree tree, int objectType, String name, int type, boolean publish){
        this(tree, objectType, name, "", type, publish);
    }
    /*
     *@param SystemTree tree.
     *@param int objectType.
     *@param String name.
     *@param int type.
     */
    public TestObject(SystemTree tree, int objectType, String name, int type){
        this(tree, objectType, name, "", 4, false);
    }
    
    public int getId(){
        return this.id;
    }
    
    public int getObjectType(){
        return this.objectType;
    }
    public String getObjectTypeString(){
        if(this.objectType==0)
            return "theme";
        else if(this.objectType==1)
            return "series";
        else if(this.objectType==2)
            return "question";
        else
            return "answer";
    }
    
    public int getType(){
        if(this.objectType==2||this.objectType==3)
            return this.type;
        else
            return 100;
    }
    public String getTypeString(){
        if(this.objectType==2){
            if(this.type==0)
                return "one-from-lot";
            else if(this.type==1)
                return "some-from-lot";
            else if(this.type==2)
                return "user-variant";
        }
        else if(this.objectType==3){
            if(this.type==0)
                return "false";
            else
                return "true";
        }
        else{
            return "";
        }
        return "";
    }
    
    public String getName(){
        return this.name;
    }
    public void setName(String name){
        this.name = name;
    }
    
    public String getDescription(){
        if(this.objectType==2||this.objectType==3)
            return "";
        else
            return this.description;
    }
    public void setDescription(String description){
        this.description = description;
    }
    
    public boolean isPublish(){
        return this.publish;
    }
    public void setPublish(boolean publish){
        this.publish = publish;
    }
    
    public boolean isRoot(){
        return this.root;
    }
    public void setRoot(boolean param){
        this.root = param;
    }
    /*
     *Описываем компаратор
     *Если тип неравен - возвращаем отрицательное значение
     *Если тип равен но неравен id - положительное.
     **/
    public int compareTo(TestObject obj){
        if(this.getObjectType()!=obj.getObjectType()){
            return -1;
        }
        else{
            if(this.getId()!=obj.getId())
                return 1;
            else
                return 0;
        }
    }
}



и расширенный класс дерева

c
Код

lass SystemTree extends JTree{
    private int themeId = 0;
    private int seriesId = 0;
    private int questionId = 0;
    private int answerId = 0;
    
    private TestObject currentObject;
    private MainPanel mainPanel;
    
    public SystemTree(MainPanel containerEditor){
        super();
        DefaultTreeModel temp = (DefaultTreeModel)this.getDefaultTreeModel();
        temp.setRoot(new TestObject(this, TestObject.ROOT, "Тесты", 4));
        this.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        //this.addTreeSelectionListener(new TreeSelectHandler(this));
        this.setToggleClickCount(5000);
        this.mainPanel = containerEditor;
    }
    
    public int getThemeId(){
        this.themeId++;
        return this.themeId;
    }
    public int getSeriesId(){
        this.seriesId++;
        return this.seriesId;
    }
    public int getQuestionId(){
        this.questionId++;
        return this.questionId;
    }
    public int getAnswerId(){
        this.answerId++;
        return this.answerId;
    }
        
    public TestObject getCurrentObject(){
        return this.currentObject;
    }
    
   public void setCurrentObject(TestObject obj){
        this.currentObject = obj;
   }
      
   public boolean DeleteTheme(int themeId){
       return false;
   }
   public boolean DeleteSeries(int seriesId){
       return false;
   }
   public boolean DeleteQuestion(int questionId){
       return false;
   }
   public boolean DeleteAnswer(int answerId){
       return false;
   }
   public MainPanel getMainPanel(){
       return this.mainPanel;
   }
   /*
    *Функция сбрасывает все текущие объекты
    */
   public void resetCurrentObjects(){
       this.currentObject = null;
   }
}


Вот здесь ничего не получается:
Код

public SystemTree(MainPanel containerEditor){
        super();
        DefaultTreeModel temp = (DefaultTreeModel)this.getDefaultTreeModel();
        temp.setRoot(new TestObject(this, TestObject.ROOT, "Тесты", 4));
        this.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        //this.addTreeSelectionListener(new TreeSelectHandler(this));
        this.setToggleClickCount(5000);
        this.mainPanel = containerEditor;
    }



Код компилица без ошибок и предупреждений, но создаётся дерево не с моей нодой в качестве корневой, а с набором, как я понял, демо-узлов (colors, sports, food).

В общем вопрос: как правильно вставить узел в ПУСТОЕ дерево при его создании. Не передавая узел как аргумент конструктору.

Автор: philips 21.4.2008, 22:51
Цитата

Код компилица без ошибок и предупреждений, но создаётся дерево не с моей нодой в качестве корневой, а с набором, как я понял, демо-узлов (colors, sports, food).

надо снова дереву назначить модель. в твоем случае:
Код

public SystemTree(JFrame frame){
        super();
        DefaultTreeModel temp = (DefaultTreeModel)this.getDefaultTreeModel();
        temp.setRoot(new TestObject(this, TestObject.ROOT, "Тесты", 4));
        this.setModel(temp);    // <<<<<<
        ......



Автор: kaa 22.4.2008, 21:12
philips
Понял, спасибо, попробую smile

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)