summaryrefslogtreecommitdiff
path: root/platform/platform-impl/src/com/intellij/ide/plugins/AvailablePluginsTableModel.java
blob: ee538453c2e1d460718b0194f88b92618a733fa6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Created by IntelliJ IDEA.
 * User: Anna.Kozlova
 * Date: 19-Aug-2006
 * Time: 14:54:29
 */
package com.intellij.ide.plugins;

import com.intellij.openapi.application.ex.ApplicationInfoEx;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.ui.ColumnInfo;

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

/**
 * Created by IntelliJ IDEA.
 * User: stathik
 * Date: Dec 26, 2003
 * Time: 3:51:58 PM
 * To change this template use Options | File Templates.
 */
public class AvailablePluginsTableModel extends PluginTableModel {

  public static final String ALL = "All";
  private String myCategory = ALL;
  private TreeSet<String> myAvailableCategories = new TreeSet<String>();

  protected static final String STATUS = "Status";

  public static final String JETBRAINS_REPO = "JetBrains Plugin Repository";
  public static final String BUILTIN_REPO = "Built-in Plugin Repository";
  private String myRepository = ALL;
  private String myVendor = null;

  public AvailablePluginsTableModel() {
    super.columns = new ColumnInfo[] {
      new AvailablePluginColumnInfo(this),
      //new PluginManagerColumnInfo(PluginManagerColumnInfo.COLUMN_DOWNLOADS, this),
      //new PluginManagerColumnInfo(PluginManagerColumnInfo.COLUMN_RATE, this),
      //new PluginManagerColumnInfo(PluginManagerColumnInfo.COLUMN_DATE, this)
      /*,
      new PluginManagerColumnInfo(PluginManagerColumnInfo.COLUMN_CATEGORY, this)*/};

    setSortKey(new RowSorter.SortKey(getNameColumn(), SortOrder.ASCENDING));
    view = new ArrayList<IdeaPluginDescriptor>();
  }

  public String getCategory() {
    return myCategory;
  }

  public void setCategory(String category, String filter) {
    myCategory = category;
    filter(filter);
  }

  public void setRepository(String repository, String filter) {
    myRepository = repository;
    filter(filter);
  }

  public void setVendor(String vendor) {
    myVendor = vendor;
    filter("");
  }

  @Override
  public boolean isPluginDescriptorAccepted(IdeaPluginDescriptor descriptor) {
    final String category = descriptor.getCategory();
    if (category != null){
      if (!ALL.equals(myCategory) && !category.equals(myCategory)) return false;
    }

    if (myVendor != null) {
      final String vendor = descriptor.getVendor();
      if (vendor == null || !StringUtil.containsIgnoreCase(vendor, myVendor)) {
        return false;
      }
    }

    return isHostAccepted(((PluginNode)descriptor).getRepositoryName());
  }

  public boolean isHostAccepted(String repositoryName) {
    if (repositoryName != null) {
      if (repositoryName.equals(ApplicationInfoEx.getInstanceEx().getBuiltinPluginsUrl()) && myRepository.equals(BUILTIN_REPO)) {
        return true;
      }
      else if (!ALL.equals(myRepository) && !repositoryName.equals(myRepository)) {
        return false;
      }
    }
    else {
      return ALL.equals(myRepository) || JETBRAINS_REPO.equals(myRepository);
    }
    return true;
  }

  public TreeSet<String> getAvailableCategories() {
    return myAvailableCategories;
  }

  public String getRepository() {
    return myRepository;
  }

  private static void updateStatus(final IdeaPluginDescriptor descr) {
    if (descr instanceof PluginNode) {
      final PluginNode node = (PluginNode)descr;
      IdeaPluginDescriptor existing = PluginManager.getPlugin(descr.getPluginId());
      if (existing != null) {
        node.setStatus(PluginNode.STATUS_INSTALLED);
        node.setInstalledVersion(existing.getVersion());
      }
    }
  }

  public void updatePluginsList(List<IdeaPluginDescriptor> list) {
    view.clear();
    myAvailableCategories.clear();
    filtered.clear();

    //  For each downloadable plugin we need to know whether its counterpart
    //  is already installed, and if yes compare the difference in versions:
    //  availability of newer versions will be indicated separately.
    for (IdeaPluginDescriptor descr : list) {
      updateStatus(descr);
      view.add(descr);
      final String category = descr.getCategory();
      if (category != null) {
        myAvailableCategories.add(category);
      } else {
        myAvailableCategories.add(AvailablePluginsManagerMain.N_A);
      }
    }

    fireTableDataChanged();
  }

  @Override
  public void filter(final List<IdeaPluginDescriptor> filtered) {
    view.clear();
    for (IdeaPluginDescriptor descriptor : filtered) {
      view.add(descriptor);
    }
    super.filter(filtered);
  }

  public int getNameColumn() {
    return 0;
  }

}