Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c83715dcdc2d6fad91fe016da1b6fd1d04977f9a (plain) (blame)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * Copyright (c) 2015, 2016 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.explorer.ui.checkouts;

import org.eclipse.emf.cdo.CDOElement;
import org.eclipse.emf.cdo.common.commit.CDOCommitInfo;
import org.eclipse.emf.cdo.common.util.CDORenameContext;
import org.eclipse.emf.cdo.eresource.CDOResourceNode;
import org.eclipse.emf.cdo.explorer.CDOExplorerManager.ElementsChangedEvent;
import org.eclipse.emf.cdo.explorer.CDOExplorerUtil;
import org.eclipse.emf.cdo.explorer.checkouts.CDOCheckout;
import org.eclipse.emf.cdo.explorer.ui.bundle.OM;
import org.eclipse.emf.cdo.explorer.ui.properties.ExplorerUIAdapterFactory;
import org.eclipse.emf.cdo.internal.explorer.checkouts.CDOCheckoutManagerImpl;
import org.eclipse.emf.cdo.transaction.CDOTransaction;

import org.eclipse.net4j.util.StringUtil;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;

/**
 * @author Eike Stepper
 */
public class ResourceGroup extends CDOElement
{
  private String name;

  public ResourceGroup(CDOResourceNode delegate)
  {
    super(delegate);
    reset();
  }

  @Override
  public CDOResourceNode getDelegate()
  {
    return (CDOResourceNode)super.getDelegate();
  }

  @Override
  public void reset()
  {
    super.reset();
    name = getDelegate().trimExtension();
  }

  @Override
  public Object getAdapter(@SuppressWarnings("rawtypes") Class adapter)
  {
    if (adapter == CDORenameContext.class)
    {
      CDOResourceNode delegate = getDelegate();
      CDOCheckout checkout = CDOExplorerUtil.getCheckout(delegate);
      if (checkout != null && checkout.isOpen() && !checkout.isReadOnly())
      {
        return new CDORenameContext()
        {

          public String getType()
          {
            return "Resource Group";
          }

          public String getName()
          {
            return name;
          }

          public void setName(final String name)
          {
            final String type = getType();
            final String title = "Rename " + type.toLowerCase();

            new Job(title)
            {
              @Override
              protected IStatus run(IProgressMonitor monitor)
              {
                CDOResourceNode resourceNode = getDelegate();
                CDOCheckout checkout = CDOExplorerUtil.getCheckout(resourceNode);
                CDOTransaction transaction = checkout.openTransaction();

                CDOCommitInfo commitInfo = null;

                try
                {
                  for (Object child : getChildren())
                  {
                    if (child instanceof CDOResourceNode)
                    {
                      CDOResourceNode childNode = (CDOResourceNode)child;
                      CDOResourceNode transactionalChildNode = transaction.getObject(childNode);
                      String extension = transactionalChildNode.getExtension();

                      transactionalChildNode.setName(name + "." + extension);
                    }
                  }

                  commitInfo = transaction.commit();
                }
                catch (Exception ex)
                {
                  OM.LOG.error(ex);
                }
                finally
                {
                  transaction.close();
                }

                if (commitInfo != null)
                {
                  if (!checkout.getView().waitForUpdate(commitInfo.getTimeStamp(), 10000))
                  {
                    OM.LOG.error(title + ": Did not receive an update");
                    return Status.OK_STATUS;
                  }

                  CDOCheckoutManagerImpl checkoutManager = (CDOCheckoutManagerImpl)CDOExplorerUtil.getCheckoutManager();
                  checkoutManager.fireElementChangedEvent(ElementsChangedEvent.StructuralImpact.PARENT, ResourceGroup.this);
                }

                return Status.OK_STATUS;
              }
            }.schedule();
          }

          public String validateName(String name)
          {
            String type = getType();
            if (StringUtil.isEmpty(name))
            {
              return type + " name is empty.";
            }

            if (name.equals(getName()))
            {
              return null;
            }

            for (Object child : getChildren())
            {
              if (child instanceof CDOResourceNode)
              {
                CDOResourceNode childNode = (CDOResourceNode)child;
                String extension = childNode.getExtension();

                String error = ExplorerUIAdapterFactory.checkUniqueName(childNode, name + "." + extension, type);
                if (error != null)
                {
                  return error;
                }
              }
            }

            return null;
          }
        };
      }
    }

    return super.getAdapter(adapter);
  }

  @Override
  public String toString(Object child)
  {
    return ((CDOResourceNode)child).getExtension();
  }

  @Override
  public String toString()
  {
    return name;
  }
}

Back to the top