You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EditRepositoryDialog.java 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.client;
  17. import java.awt.BorderLayout;
  18. import java.awt.Component;
  19. import java.awt.Dimension;
  20. import java.awt.FlowLayout;
  21. import java.awt.Font;
  22. import java.awt.GridLayout;
  23. import java.awt.Insets;
  24. import java.awt.event.ActionEvent;
  25. import java.awt.event.ActionListener;
  26. import java.awt.event.ItemEvent;
  27. import java.awt.event.ItemListener;
  28. import java.awt.event.KeyEvent;
  29. import java.text.MessageFormat;
  30. import java.util.ArrayList;
  31. import java.util.Arrays;
  32. import java.util.HashSet;
  33. import java.util.LinkedHashMap;
  34. import java.util.List;
  35. import java.util.Map;
  36. import java.util.Set;
  37. import javax.swing.BoxLayout;
  38. import javax.swing.ButtonGroup;
  39. import javax.swing.DefaultListCellRenderer;
  40. import javax.swing.ImageIcon;
  41. import javax.swing.JButton;
  42. import javax.swing.JCheckBox;
  43. import javax.swing.JComboBox;
  44. import javax.swing.JComponent;
  45. import javax.swing.JDialog;
  46. import javax.swing.JLabel;
  47. import javax.swing.JList;
  48. import javax.swing.JOptionPane;
  49. import javax.swing.JPanel;
  50. import javax.swing.JRadioButton;
  51. import javax.swing.JRootPane;
  52. import javax.swing.JScrollPane;
  53. import javax.swing.JTabbedPane;
  54. import javax.swing.JTextField;
  55. import javax.swing.KeyStroke;
  56. import javax.swing.ListCellRenderer;
  57. import javax.swing.ScrollPaneConstants;
  58. import com.gitblit.Constants.AccessRestrictionType;
  59. import com.gitblit.Constants.AuthorizationControl;
  60. import com.gitblit.Constants.FederationStrategy;
  61. import com.gitblit.Constants.RegistrantType;
  62. import com.gitblit.models.RegistrantAccessPermission;
  63. import com.gitblit.models.RepositoryModel;
  64. import com.gitblit.utils.ArrayUtils;
  65. import com.gitblit.utils.StringUtils;
  66. /**
  67. * Dialog to create/edit a repository.
  68. *
  69. * @author James Moger
  70. */
  71. public class EditRepositoryDialog extends JDialog {
  72. private static final long serialVersionUID = 1L;
  73. private final String repositoryName;
  74. private final RepositoryModel repository;
  75. private boolean isCreate;
  76. private boolean canceled = true;
  77. private JTextField nameField;
  78. private JTextField descriptionField;
  79. private JCheckBox useDocs;
  80. private JCheckBox useIncrementalPushTags;
  81. private JCheckBox showRemoteBranches;
  82. private JCheckBox skipSizeCalculation;
  83. private JCheckBox skipSummaryMetrics;
  84. private JCheckBox isFrozen;
  85. private JTextField mailingListsField;
  86. private JComboBox accessRestriction;
  87. private JRadioButton allowAuthenticated;
  88. private JRadioButton allowNamed;
  89. private JCheckBox allowForks;
  90. private JCheckBox verifyCommitter;
  91. private JComboBox federationStrategy;
  92. private JPalette<String> ownersPalette;
  93. private JComboBox headRefField;
  94. private JComboBox gcPeriod;
  95. private JTextField gcThreshold;
  96. private JComboBox maxActivityCommits;
  97. private RegistrantPermissionsPanel usersPalette;
  98. private JPalette<String> setsPalette;
  99. private RegistrantPermissionsPanel teamsPalette;
  100. private JPalette<String> indexedBranchesPalette;
  101. private JPalette<String> preReceivePalette;
  102. private JLabel preReceiveInherited;
  103. private JPalette<String> postReceivePalette;
  104. private JLabel postReceiveInherited;
  105. private Set<String> repositoryNames;
  106. private JPanel customFieldsPanel;
  107. private List<JTextField> customTextfields;
  108. public EditRepositoryDialog(int protocolVersion) {
  109. this(protocolVersion, new RepositoryModel());
  110. this.isCreate = true;
  111. setTitle(Translation.get("gb.newRepository"));
  112. }
  113. public EditRepositoryDialog(int protocolVersion, RepositoryModel aRepository) {
  114. super();
  115. this.repositoryName = aRepository.name;
  116. this.repository = new RepositoryModel();
  117. this.repositoryNames = new HashSet<String>();
  118. this.isCreate = false;
  119. initialize(protocolVersion, aRepository);
  120. setModal(true);
  121. setResizable(false);
  122. setTitle(Translation.get("gb.edit") + ": " + aRepository.name);
  123. setIconImage(new ImageIcon(getClass()
  124. .getResource("/gitblt-favicon.png")).getImage());
  125. }
  126. @Override
  127. protected JRootPane createRootPane() {
  128. KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
  129. JRootPane rootPane = new JRootPane();
  130. rootPane.registerKeyboardAction(new ActionListener() {
  131. @Override
  132. public void actionPerformed(ActionEvent actionEvent) {
  133. setVisible(false);
  134. }
  135. }, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
  136. return rootPane;
  137. }
  138. private void initialize(int protocolVersion, RepositoryModel anRepository) {
  139. nameField = new JTextField(anRepository.name == null ? ""
  140. : anRepository.name, 35);
  141. descriptionField = new JTextField(anRepository.description == null ? ""
  142. : anRepository.description, 35);
  143. JTextField originField = new JTextField(
  144. anRepository.origin == null ? "" : anRepository.origin, 40);
  145. originField.setEditable(false);
  146. if (ArrayUtils.isEmpty(anRepository.availableRefs)) {
  147. headRefField = new JComboBox();
  148. headRefField.setEnabled(false);
  149. } else {
  150. headRefField = new JComboBox(
  151. anRepository.availableRefs.toArray());
  152. headRefField.setSelectedItem(anRepository.HEAD);
  153. }
  154. Integer [] gcPeriods = { 1, 2, 3, 4, 5, 7, 10, 14 };
  155. gcPeriod = new JComboBox(gcPeriods);
  156. gcPeriod.setSelectedItem(anRepository.gcPeriod);
  157. gcThreshold = new JTextField(8);
  158. gcThreshold.setText(anRepository.gcThreshold);
  159. ownersPalette = new JPalette<String>(true);
  160. useIncrementalPushTags = new JCheckBox(Translation.get("gb.useIncrementalPushTagsDescription"),
  161. anRepository.useIncrementalPushTags);
  162. showRemoteBranches = new JCheckBox(
  163. Translation.get("gb.showRemoteBranchesDescription"),
  164. anRepository.showRemoteBranches);
  165. skipSizeCalculation = new JCheckBox(
  166. Translation.get("gb.skipSizeCalculationDescription"),
  167. anRepository.skipSizeCalculation);
  168. skipSummaryMetrics = new JCheckBox(
  169. Translation.get("gb.skipSummaryMetricsDescription"),
  170. anRepository.skipSummaryMetrics);
  171. isFrozen = new JCheckBox(Translation.get("gb.isFrozenDescription"),
  172. anRepository.isFrozen);
  173. maxActivityCommits = new JComboBox(new Integer [] { -1, 0, 25, 50, 75, 100, 150, 250, 500 });
  174. maxActivityCommits.setSelectedItem(anRepository.maxActivityCommits);
  175. mailingListsField = new JTextField(
  176. ArrayUtils.isEmpty(anRepository.mailingLists) ? ""
  177. : StringUtils.flattenStrings(anRepository.mailingLists,
  178. " "), 50);
  179. accessRestriction = new JComboBox(AccessRestrictionType.values());
  180. accessRestriction.setRenderer(new AccessRestrictionRenderer());
  181. accessRestriction.setSelectedItem(anRepository.accessRestriction);
  182. accessRestriction.addItemListener(new ItemListener() {
  183. @Override
  184. public void itemStateChanged(ItemEvent e) {
  185. if (e.getStateChange() == ItemEvent.SELECTED) {
  186. AccessRestrictionType art = (AccessRestrictionType) accessRestriction.getSelectedItem();
  187. EditRepositoryDialog.this.setupAccessPermissions(art);
  188. }
  189. }
  190. });
  191. boolean authenticated = anRepository.authorizationControl != null
  192. && AuthorizationControl.AUTHENTICATED.equals(anRepository.authorizationControl);
  193. allowAuthenticated = new JRadioButton(Translation.get("gb.allowAuthenticatedDescription"));
  194. allowAuthenticated.setSelected(authenticated);
  195. allowAuthenticated.addItemListener(new ItemListener() {
  196. @Override
  197. public void itemStateChanged(ItemEvent e) {
  198. if (e.getStateChange() == ItemEvent.SELECTED) {
  199. usersPalette.setEnabled(false);
  200. teamsPalette.setEnabled(false);
  201. }
  202. }
  203. });
  204. allowNamed = new JRadioButton(Translation.get("gb.allowNamedDescription"));
  205. allowNamed.setSelected(!authenticated);
  206. allowNamed.addItemListener(new ItemListener() {
  207. @Override
  208. public void itemStateChanged(ItemEvent e) {
  209. if (e.getStateChange() == ItemEvent.SELECTED) {
  210. usersPalette.setEnabled(true);
  211. teamsPalette.setEnabled(true);
  212. }
  213. }
  214. });
  215. ButtonGroup group = new ButtonGroup();
  216. group.add(allowAuthenticated);
  217. group.add(allowNamed);
  218. JPanel authorizationPanel = new JPanel(new GridLayout(0, 1));
  219. authorizationPanel.add(allowAuthenticated);
  220. authorizationPanel.add(allowNamed);
  221. allowForks = new JCheckBox(Translation.get("gb.allowForksDescription"), anRepository.allowForks);
  222. verifyCommitter = new JCheckBox(Translation.get("gb.verifyCommitterDescription"), anRepository.verifyCommitter);
  223. // federation strategies - remove ORIGIN choice if this repository has
  224. // no origin.
  225. List<FederationStrategy> federationStrategies = new ArrayList<FederationStrategy>(
  226. Arrays.asList(FederationStrategy.values()));
  227. if (StringUtils.isEmpty(anRepository.origin)) {
  228. federationStrategies.remove(FederationStrategy.FEDERATE_ORIGIN);
  229. }
  230. federationStrategy = new JComboBox(federationStrategies.toArray());
  231. federationStrategy.setRenderer(new FederationStrategyRenderer());
  232. federationStrategy.setSelectedItem(anRepository.federationStrategy);
  233. JPanel fieldsPanel = new JPanel(new GridLayout(0, 1));
  234. fieldsPanel.add(newFieldPanel(Translation.get("gb.name"), nameField));
  235. fieldsPanel.add(newFieldPanel(Translation.get("gb.description"),
  236. descriptionField));
  237. fieldsPanel
  238. .add(newFieldPanel(Translation.get("gb.origin"), originField));
  239. fieldsPanel.add(newFieldPanel(Translation.get("gb.headRef"), headRefField));
  240. fieldsPanel.add(newFieldPanel(Translation.get("gb.gcPeriod"), gcPeriod));
  241. fieldsPanel.add(newFieldPanel(Translation.get("gb.gcThreshold"), gcThreshold));
  242. fieldsPanel
  243. .add(newFieldPanel(Translation.get("gb.enableDocs"), useDocs));
  244. fieldsPanel
  245. .add(newFieldPanel(Translation.get("gb.enableIncrementalPushTags"), useIncrementalPushTags));
  246. fieldsPanel.add(newFieldPanel(Translation.get("gb.showRemoteBranches"),
  247. showRemoteBranches));
  248. fieldsPanel
  249. .add(newFieldPanel(Translation.get("gb.skipSizeCalculation"),
  250. skipSizeCalculation));
  251. fieldsPanel.add(newFieldPanel(Translation.get("gb.skipSummaryMetrics"),
  252. skipSummaryMetrics));
  253. fieldsPanel.add(newFieldPanel(Translation.get("gb.maxActivityCommits"),
  254. maxActivityCommits));
  255. fieldsPanel.add(newFieldPanel(Translation.get("gb.mailingLists"),
  256. mailingListsField));
  257. JPanel clonePushPanel = new JPanel(new GridLayout(0, 1));
  258. clonePushPanel
  259. .add(newFieldPanel(Translation.get("gb.isFrozen"), isFrozen));
  260. clonePushPanel
  261. .add(newFieldPanel(Translation.get("gb.allowForks"), allowForks));
  262. clonePushPanel
  263. .add(newFieldPanel(Translation.get("gb.verifyCommitter"), verifyCommitter));
  264. usersPalette = new RegistrantPermissionsPanel(RegistrantType.USER);
  265. JPanel northFieldsPanel = new JPanel(new BorderLayout(0, 5));
  266. northFieldsPanel.add(newFieldPanel(Translation.get("gb.owners"), ownersPalette), BorderLayout.NORTH);
  267. northFieldsPanel.add(newFieldPanel(Translation.get("gb.accessRestriction"),
  268. accessRestriction), BorderLayout.CENTER);
  269. JPanel northAccessPanel = new JPanel(new BorderLayout(5, 5));
  270. northAccessPanel.add(northFieldsPanel, BorderLayout.NORTH);
  271. northAccessPanel.add(newFieldPanel(Translation.get("gb.authorizationControl"),
  272. authorizationPanel), BorderLayout.CENTER);
  273. northAccessPanel.add(clonePushPanel, BorderLayout.SOUTH);
  274. JPanel accessPanel = new JPanel(new BorderLayout(5, 5));
  275. accessPanel.add(northAccessPanel, BorderLayout.NORTH);
  276. accessPanel.add(newFieldPanel(Translation.get("gb.userPermissions"),
  277. usersPalette), BorderLayout.CENTER);
  278. teamsPalette = new RegistrantPermissionsPanel(RegistrantType.TEAM);
  279. JPanel teamsPanel = new JPanel(new BorderLayout(5, 5));
  280. teamsPanel.add(
  281. newFieldPanel(Translation.get("gb.teamPermissions"),
  282. teamsPalette), BorderLayout.CENTER);
  283. setsPalette = new JPalette<String>();
  284. JPanel federationPanel = new JPanel(new BorderLayout(5, 5));
  285. federationPanel.add(
  286. newFieldPanel(Translation.get("gb.federationStrategy"),
  287. federationStrategy), BorderLayout.NORTH);
  288. federationPanel
  289. .add(newFieldPanel(Translation.get("gb.federationSets"),
  290. setsPalette), BorderLayout.CENTER);
  291. indexedBranchesPalette = new JPalette<String>();
  292. JPanel indexedBranchesPanel = new JPanel(new BorderLayout(5, 5));
  293. indexedBranchesPanel
  294. .add(newFieldPanel(Translation.get("gb.indexedBranches"),
  295. indexedBranchesPalette), BorderLayout.CENTER);
  296. preReceivePalette = new JPalette<String>(true);
  297. preReceiveInherited = new JLabel();
  298. JPanel preReceivePanel = new JPanel(new BorderLayout(5, 5));
  299. preReceivePanel.add(preReceivePalette, BorderLayout.CENTER);
  300. preReceivePanel.add(preReceiveInherited, BorderLayout.WEST);
  301. postReceivePalette = new JPalette<String>(true);
  302. postReceiveInherited = new JLabel();
  303. JPanel postReceivePanel = new JPanel(new BorderLayout(5, 5));
  304. postReceivePanel.add(postReceivePalette, BorderLayout.CENTER);
  305. postReceivePanel.add(postReceiveInherited, BorderLayout.WEST);
  306. customFieldsPanel = new JPanel();
  307. customFieldsPanel.setLayout(new BoxLayout(customFieldsPanel, BoxLayout.Y_AXIS));
  308. JScrollPane customFieldsScrollPane = new JScrollPane(customFieldsPanel);
  309. customFieldsScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  310. customFieldsScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
  311. JTabbedPane panel = new JTabbedPane(JTabbedPane.TOP);
  312. panel.addTab(Translation.get("gb.general"), fieldsPanel);
  313. panel.addTab(Translation.get("gb.accessRestriction"), accessPanel);
  314. if (protocolVersion >= 2) {
  315. panel.addTab(Translation.get("gb.teams"), teamsPanel);
  316. }
  317. panel.addTab(Translation.get("gb.federation"), federationPanel);
  318. if (protocolVersion >= 3) {
  319. panel.addTab(Translation.get("gb.indexedBranches"), indexedBranchesPanel);
  320. }
  321. panel.addTab(Translation.get("gb.preReceiveScripts"), preReceivePanel);
  322. panel.addTab(Translation.get("gb.postReceiveScripts"), postReceivePanel);
  323. panel.addTab(Translation.get("gb.customFields"), customFieldsScrollPane);
  324. setupAccessPermissions(anRepository.accessRestriction);
  325. JButton createButton = new JButton(Translation.get("gb.save"));
  326. createButton.addActionListener(new ActionListener() {
  327. @Override
  328. public void actionPerformed(ActionEvent event) {
  329. if (validateFields()) {
  330. canceled = false;
  331. setVisible(false);
  332. }
  333. }
  334. });
  335. JButton cancelButton = new JButton(Translation.get("gb.cancel"));
  336. cancelButton.addActionListener(new ActionListener() {
  337. @Override
  338. public void actionPerformed(ActionEvent event) {
  339. canceled = true;
  340. setVisible(false);
  341. }
  342. });
  343. JPanel controls = new JPanel();
  344. controls.add(cancelButton);
  345. controls.add(createButton);
  346. final Insets _insets = new Insets(5, 5, 5, 5);
  347. JPanel centerPanel = new JPanel(new BorderLayout(5, 5)) {
  348. private static final long serialVersionUID = 1L;
  349. @Override
  350. public Insets getInsets() {
  351. return _insets;
  352. }
  353. };
  354. centerPanel.add(panel, BorderLayout.CENTER);
  355. centerPanel.add(controls, BorderLayout.SOUTH);
  356. getContentPane().setLayout(new BorderLayout(5, 5));
  357. getContentPane().add(centerPanel, BorderLayout.CENTER);
  358. pack();
  359. nameField.requestFocus();
  360. }
  361. private JPanel newFieldPanel(String label, JComponent comp) {
  362. return newFieldPanel(label, 150, comp);
  363. }
  364. private JPanel newFieldPanel(String label, int labelSize, JComponent comp) {
  365. JLabel fieldLabel = new JLabel(label);
  366. fieldLabel.setFont(fieldLabel.getFont().deriveFont(Font.BOLD));
  367. fieldLabel.setPreferredSize(new Dimension(labelSize, 20));
  368. JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
  369. panel.add(fieldLabel);
  370. panel.add(comp);
  371. return panel;
  372. }
  373. private void setupAccessPermissions(AccessRestrictionType art) {
  374. if (AccessRestrictionType.NONE.equals(art)) {
  375. usersPalette.setEnabled(false);
  376. teamsPalette.setEnabled(false);
  377. allowAuthenticated.setEnabled(false);
  378. allowNamed.setEnabled(false);
  379. verifyCommitter.setEnabled(false);
  380. } else {
  381. allowAuthenticated.setEnabled(true);
  382. allowNamed.setEnabled(true);
  383. verifyCommitter.setEnabled(true);
  384. if (allowNamed.isSelected()) {
  385. usersPalette.setEnabled(true);
  386. teamsPalette.setEnabled(true);
  387. }
  388. }
  389. }
  390. private boolean validateFields() {
  391. String rname = nameField.getText();
  392. if (StringUtils.isEmpty(rname)) {
  393. error("Please enter a repository name!");
  394. return false;
  395. }
  396. // automatically convert backslashes to forward slashes
  397. rname = rname.replace('\\', '/');
  398. // Automatically replace // with /
  399. rname = rname.replace("//", "/");
  400. // prohibit folder paths
  401. if (rname.startsWith("/")) {
  402. error("Leading root folder references (/) are prohibited.");
  403. return false;
  404. }
  405. if (rname.startsWith("../")) {
  406. error("Relative folder references (../) are prohibited.");
  407. return false;
  408. }
  409. if (rname.contains("/../")) {
  410. error("Relative folder references (../) are prohibited.");
  411. return false;
  412. }
  413. if (rname.endsWith("/")) {
  414. rname = rname.substring(0, rname.length() - 1);
  415. }
  416. // confirm valid characters in repository name
  417. Character c = StringUtils.findInvalidCharacter(rname);
  418. if (c != null) {
  419. error(MessageFormat.format(
  420. "Illegal character ''{0}'' in repository name!", c));
  421. return false;
  422. }
  423. // verify repository name uniqueness on create
  424. if (isCreate) {
  425. // force repo names to lowercase
  426. // this means that repository name checking for rpc creation
  427. // is case-insensitive, regardless of the Gitblit server's
  428. // filesystem
  429. if (repositoryNames.contains(rname.toLowerCase())) {
  430. error(MessageFormat
  431. .format("Can not create repository ''{0}'' because it already exists.",
  432. rname));
  433. return false;
  434. }
  435. } else {
  436. // check rename collision
  437. if (!repositoryName.equalsIgnoreCase(rname)) {
  438. if (repositoryNames.contains(rname.toLowerCase())) {
  439. error(MessageFormat
  440. .format("Failed to rename ''{0}'' because ''{1}'' already exists.",
  441. repositoryName, rname));
  442. return false;
  443. }
  444. }
  445. }
  446. if (accessRestriction.getSelectedItem() == null) {
  447. error("Please select access restriction!");
  448. return false;
  449. }
  450. if (federationStrategy.getSelectedItem() == null) {
  451. error("Please select federation strategy!");
  452. return false;
  453. }
  454. repository.name = rname;
  455. repository.description = descriptionField.getText();
  456. repository.owners.clear();
  457. repository.owners.addAll(ownersPalette.getSelections());
  458. repository.HEAD = headRefField.getSelectedItem() == null ? null
  459. : headRefField.getSelectedItem().toString();
  460. repository.gcPeriod = (Integer) gcPeriod.getSelectedItem();
  461. repository.gcThreshold = gcThreshold.getText();
  462. repository.useIncrementalPushTags = useIncrementalPushTags.isSelected();
  463. repository.showRemoteBranches = showRemoteBranches.isSelected();
  464. repository.skipSizeCalculation = skipSizeCalculation.isSelected();
  465. repository.skipSummaryMetrics = skipSummaryMetrics.isSelected();
  466. repository.maxActivityCommits = (Integer) maxActivityCommits.getSelectedItem();
  467. repository.isFrozen = isFrozen.isSelected();
  468. repository.allowForks = allowForks.isSelected();
  469. repository.verifyCommitter = verifyCommitter.isSelected();
  470. String ml = mailingListsField.getText();
  471. if (!StringUtils.isEmpty(ml)) {
  472. Set<String> list = new HashSet<String>();
  473. for (String address : ml.split("(,|\\s)")) {
  474. if (StringUtils.isEmpty(address)) {
  475. continue;
  476. }
  477. list.add(address.toLowerCase());
  478. }
  479. repository.mailingLists = new ArrayList<String>(list);
  480. }
  481. repository.accessRestriction = (AccessRestrictionType) accessRestriction
  482. .getSelectedItem();
  483. repository.authorizationControl = allowAuthenticated.isSelected() ?
  484. AuthorizationControl.AUTHENTICATED : AuthorizationControl.NAMED;
  485. repository.federationStrategy = (FederationStrategy) federationStrategy
  486. .getSelectedItem();
  487. if (repository.federationStrategy.exceeds(FederationStrategy.EXCLUDE)) {
  488. repository.federationSets = setsPalette.getSelections();
  489. }
  490. repository.indexedBranches = indexedBranchesPalette.getSelections();
  491. repository.preReceiveScripts = preReceivePalette.getSelections();
  492. repository.postReceiveScripts = postReceivePalette.getSelections();
  493. // Custom Fields
  494. repository.customFields = new LinkedHashMap<String, String>();
  495. if (customTextfields != null) {
  496. for (JTextField field : customTextfields) {
  497. String key = field.getName();
  498. String value = field.getText();
  499. repository.customFields.put(key, value);
  500. }
  501. }
  502. return true;
  503. }
  504. private void error(String message) {
  505. JOptionPane.showMessageDialog(EditRepositoryDialog.this, message,
  506. Translation.get("gb.error"), JOptionPane.ERROR_MESSAGE);
  507. }
  508. public void setAccessRestriction(AccessRestrictionType restriction) {
  509. this.accessRestriction.setSelectedItem(restriction);
  510. setupAccessPermissions(restriction);
  511. }
  512. public void setAuthorizationControl(AuthorizationControl authorization) {
  513. boolean authenticated = authorization != null && AuthorizationControl.AUTHENTICATED.equals(authorization);
  514. this.allowAuthenticated.setSelected(authenticated);
  515. this.allowNamed.setSelected(!authenticated);
  516. }
  517. public void setUsers(List<String> owners, List<String> all, List<RegistrantAccessPermission> permissions) {
  518. ownersPalette.setObjects(all, owners);
  519. usersPalette.setObjects(all, permissions);
  520. }
  521. public void setTeams(List<String> all, List<RegistrantAccessPermission> permissions) {
  522. teamsPalette.setObjects(all, permissions);
  523. }
  524. public void setRepositories(List<RepositoryModel> repositories) {
  525. repositoryNames.clear();
  526. for (RepositoryModel repository : repositories) {
  527. // force repo names to lowercase
  528. // this means that repository name checking for rpc creation
  529. // is case-insensitive, regardless of the Gitblit server's
  530. // filesystem
  531. repositoryNames.add(repository.name.toLowerCase());
  532. }
  533. }
  534. public void setFederationSets(List<String> all, List<String> selected) {
  535. setsPalette.setObjects(all, selected);
  536. }
  537. public void setIndexedBranches(List<String> all, List<String> selected) {
  538. indexedBranchesPalette.setObjects(all, selected);
  539. }
  540. public void setPreReceiveScripts(List<String> all, List<String> inherited,
  541. List<String> selected) {
  542. preReceivePalette.setObjects(all, selected);
  543. showInherited(inherited, preReceiveInherited);
  544. }
  545. public void setPostReceiveScripts(List<String> all, List<String> inherited,
  546. List<String> selected) {
  547. postReceivePalette.setObjects(all, selected);
  548. showInherited(inherited, postReceiveInherited);
  549. }
  550. private void showInherited(List<String> list, JLabel label) {
  551. StringBuilder sb = new StringBuilder();
  552. if (list != null && list.size() > 0) {
  553. sb.append("<html><body><b>INHERITED</b><ul style=\"margin-left:5px;list-style-type: none;\">");
  554. for (String script : list) {
  555. sb.append("<li>").append(script).append("</li>");
  556. }
  557. sb.append("</ul></body></html>");
  558. }
  559. label.setText(sb.toString());
  560. }
  561. public RepositoryModel getRepository() {
  562. if (canceled) {
  563. return null;
  564. }
  565. return repository;
  566. }
  567. public List<RegistrantAccessPermission> getUserAccessPermissions() {
  568. return usersPalette.getPermissions();
  569. }
  570. public List<RegistrantAccessPermission> getTeamAccessPermissions() {
  571. return teamsPalette.getPermissions();
  572. }
  573. public void setCustomFields(RepositoryModel repository, Map<String, String> customFields) {
  574. customFieldsPanel.removeAll();
  575. customTextfields = new ArrayList<JTextField>();
  576. final Insets insets = new Insets(5, 5, 5, 5);
  577. JPanel fields = new JPanel(new GridLayout(0, 1, 0, 5)) {
  578. private static final long serialVersionUID = 1L;
  579. @Override
  580. public Insets getInsets() {
  581. return insets;
  582. }
  583. };
  584. for (Map.Entry<String, String> entry : customFields.entrySet()) {
  585. String field = entry.getKey();
  586. String value = "";
  587. if (repository.customFields != null && repository.customFields.containsKey(field)) {
  588. value = repository.customFields.get(field);
  589. }
  590. JTextField textField = new JTextField(value);
  591. textField.setName(field);
  592. textField.setPreferredSize(new Dimension(450, 26));
  593. fields.add(newFieldPanel(entry.getValue(), 250, textField));
  594. customTextfields.add(textField);
  595. }
  596. JScrollPane jsp = new JScrollPane(fields);
  597. jsp.getVerticalScrollBar().setBlockIncrement(100);
  598. jsp.getVerticalScrollBar().setUnitIncrement(100);
  599. jsp.setViewportBorder(null);
  600. customFieldsPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
  601. customFieldsPanel.add(jsp);
  602. }
  603. /**
  604. * ListCellRenderer to display descriptive text about the access
  605. * restriction.
  606. *
  607. */
  608. private class AccessRestrictionRenderer extends DefaultListCellRenderer {
  609. private static final long serialVersionUID = 1L;
  610. @Override
  611. public Component getListCellRendererComponent(JList list, Object value,
  612. int index, boolean isSelected, boolean cellHasFocus) {
  613. super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
  614. if (value instanceof AccessRestrictionType) {
  615. AccessRestrictionType restriction = (AccessRestrictionType) value;
  616. switch (restriction) {
  617. case NONE:
  618. setText(Translation.get("gb.notRestricted"));
  619. break;
  620. case PUSH:
  621. setText(Translation.get("gb.pushRestricted"));
  622. break;
  623. case CLONE:
  624. setText(Translation.get("gb.cloneRestricted"));
  625. break;
  626. case VIEW:
  627. setText(Translation.get("gb.viewRestricted"));
  628. break;
  629. }
  630. } else {
  631. setText(value.toString());
  632. }
  633. return this;
  634. }
  635. }
  636. /**
  637. * ListCellRenderer to display descriptive text about the federation
  638. * strategy.
  639. */
  640. private class FederationStrategyRenderer extends JLabel implements
  641. ListCellRenderer {
  642. private static final long serialVersionUID = 1L;
  643. @Override
  644. public Component getListCellRendererComponent(JList list, Object value,
  645. int index, boolean isSelected, boolean cellHasFocus) {
  646. if (value instanceof FederationStrategy) {
  647. FederationStrategy strategy = (FederationStrategy) value;
  648. switch (strategy) {
  649. case EXCLUDE:
  650. setText(Translation.get("gb.excludeFromFederation"));
  651. break;
  652. case FEDERATE_THIS:
  653. setText(Translation.get("gb.federateThis"));
  654. break;
  655. case FEDERATE_ORIGIN:
  656. setText(Translation.get("gb.federateOrigin"));
  657. break;
  658. }
  659. } else {
  660. setText(value.toString());
  661. }
  662. return this;
  663. }
  664. }
  665. }